ver*_*one 5 java android listview android-adapter
ListView我的应用程序代码中有一个,点击时使用一个AdapterView.OnItemClickListener来检测点击次数.问题是,当我点击某个项目时,该项目的背景变为白色,而不是默认的橙色.像这样:
另外,当我不使用AdapterView时,点击的项目会变成橙色而没有任何问题.如何使点击的项目的背景再次变为橙色?
编辑:
列表布局:main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#00000000">
<!-- ListView (grid_items) -->
<ListView android:id="@+id/listview"
android:layout_height="fill_parent"
android:textSize="15px"
android:layout_width="fill_parent"
android:background="#00000000">
</ListView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
的onCreate():
public void onCreate(Bundle savedInstanceState) {try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv= (ListView)findViewById(R.id.listview);
lv.setBackgroundColor(Color.TRANSPARENT);
lv.setCacheColorHint(Color.TRANSPARENT);
//......calculations
for(int q = 0; q <v; q++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("col_1", array[q]);
fillMaps.add(map);
lv.setOnItemClickListener(onListClick);
}
//......calculations
}
Run Code Online (Sandbox Code Playgroud)
适配器视图:
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
lv.setBackgroundColor(Color.TRANSPARENT);
lv.setCacheColorHint(Color.TRANSPARENT);
//.....calculations
}
Run Code Online (Sandbox Code Playgroud)
使用自定义主题:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
自定义列表视图项的最佳方法是在xml文件中创建它们,然后使用BaseAdapter或任何类型的列表适配器,膨胀该.xml布局并填充您的数据.为了自定义高亮动画,您只需为项目的不同状态创建一个Drawable状态.
所以你去一个"新的xml文件" - >"资源类型drawable" - >"形状"命名它并完成你会看到这段代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
</shape>
Run Code Online (Sandbox Code Playgroud)
让我们为ListItem按下状态创建一个背景:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#86d47f" this is your custom color />
</shape>
Run Code Online (Sandbox Code Playgroud)
和非按下状态是另一个文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后是一个列表选择器文件,用作listview项目的背景:转到"new xml file" - >"drawable" - >"list selector"将其命名为"item_background" - >完成
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="300" !!this is the fade duration time for your animation>
<item android:drawable="@drawable/bg_drawable" android:state_pressed="true"></item>
<item android:drawable="@drawable/transparend"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后为项创建一个xml文件,根据需要自定义它,但是对于主布局集
android:background="@drawable/item_background"
Run Code Online (Sandbox Code Playgroud)
在这里,一切都很完美......这是我的适配器类:
public class ListAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
public ListAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String str_value = this.getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
tv.setText(str_value);
return convertView;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,你应该把它标记为答案..

| 归档时间: |
|
| 查看次数: |
1200 次 |
| 最近记录: |