gpt4 book ai didi

android - 如何自定义 ListView 行android

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:39 25 4
gpt4 key购买 nike

我有一个姓名列表,我想根据姓名开头的字母更改行的颜色。这就是我用来显示 ListView 的内容:

ma​​in.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ListView android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

lisviewrows.xml

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>

ma​​in.java

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

List<String> words = populateList();
List<String> l = removeDoubles(words);
Collections.sort(l);

ListView lv = (ListView)findViewById(R.id.listview);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.listviewrows, l);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

}
});
}

是否有一些有用的链接或示例可以提供给我?

谢谢!!

最佳答案

为此,您需要创建自己的自定义适配器。在 getView 方法(它返回要为每个列表项显示的 View )中,将您想要的颜色应用于 TextView 的背景。customAdapters 的妙处在于你可以做任何事情,并为你的列表项显示更复杂的 View ,因为你不再局限于 TextViews,你可以将你的列表项 XML 布局更改为任何类型的 View /布局。 .

像这样:

MyAdapter.java

public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<String> data;

public MyAdapter(Context context, ArrayList<String> data){
// Caches the LayoutInflater for quicker use
this.inflater = LayoutInflater.from(context);
// Sets the events data
this.data= data;
}

public int getCount() {
return this.data.size();
}

public String getItem(int position) throws IndexOutOfBoundsException{
return this.data.get(position);
}

public long getItemId(int position) throws IndexOutOfBoundsException{
if(position < getCount() && position >= 0 ){
return position;
}
}

public int getViewTypeCount(){
return 1;
}

public View getView(int position, View convertView, ViewGroup parent){
String myText = getItem(position);

if(convertView == null){ // If the View is not cached
// Inflates the Common View from XML file
convertView = this.inflater.inflate(R.id.my_row_layout, null);
}

// Select your color and apply it to your textview
int myColor;
if(myText.substring(0, 1) == "a"){
myColor = Color.BLACK;
}else{
....
}

convertView.findViewById(R.id.myTextViewId).setBackground(myColor);
// Of course you will need to set the same ID in your item list XML layout.

return convertView;
}
}

然后在您的 Activity 中像这样设置适配器:

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

List<String> words = populateList();
List<String> l = removeDoubles(words);
Collections.sort(l);

ListView lv = (ListView)findViewById(R.id.listview);

MyAdapter adapter = new MyAdapter(getApplicationContext(), l);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

}
});
}

关于android - 如何自定义 ListView 行android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4407865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com