リストビューの行の背景色を変更したい。今私はこれをやっています
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
view.setBackgroundColor(Color.parseColor("#ff0000"));//
}
これは機能しますが、リストビューを下にスクロールして背景色が変更された行が見えないようにしてから上にスクロールすると、行の色は変更されなくなります。
これが起こらないように、行の背景色を永続的に設定する方法はありますか?
あなたがしなければならないことは、カスタムを作ることであるListView
あなた自身とrow_layout.xml
、ファイルCustomListViewObject
があることは、boolean
要素がクリックされたかどうか、そして最後に、されたかどうかを確認するためにCustomListView
、行要素が選択されているかどうかをチェックします。
行レイアウトrow_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12dp"
android:text="Large Text"
android:id="@+id/textfield"/>
</LinearLayout>
行クラスは次のようになります。
public class CustomRowClass {
private String input;
private boolean selected;
public BallotEntryClass() {
}
public BallotEntryClass(String input, boolean selected) {
this.input = input;
this.selected = selected;
}
//setters and getters here
}
そして最後に、あなたのアダプター:
public class CustomListViewAdapter extends BaseAdapter {
private ArrayList<CustomRowClass> listData;
private LayoutInflater layoutInflater;
private Context context;
public CustomListViewAdapterBallot(Context context, ArrayList<CustomRowClass> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.input = (TextView) convertView.findViewById(R.id.textfield);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.textfield.setText(listData.get(position).getInput());
//check here if state = 1 which means selected state
if(listData.get(position).isSelected()){
convertView.setBackgroundColor(Color.GREEN);
}
else{
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
static class ViewHolder {
TextView input;
}
}
以上です。これを使用するには、次のようにします。
ListView sampleListView = (ListView) findViewById(R.id.listView);
//create your listView with your custom object
ArrayList<CustomRowClass> data = new ArrayList<>();
for(int i = 0 ; i < 10 ; i ++){
CustomRowClass entry = new CustomRowClass("Name " + i, false);
data.add(entry);
}
//create your adapter, use the nameAndAgeList ArrayList
CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, data);
//get your listView and use your adapter
listView.setAdapter(sampleListView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CustomRowClass selection = (CustomRowClass) listView.getItemAtPosition(position);
selection.setSelected(true);
adapter.notifyDataSetChanged();
}
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加