默认情况下,我想使用setItemChecked突出显示列表项。 checkedItemPosition设置了checkedItemPosition,但未突出显示项目列表。
这是我的阵列适配器代码:
class MyListAdapter(
context: Context,
private val myItemList: List<Mytem>)
: ArrayAdapter<Mytem>(context, R.layout.item_simple, myItemList) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView: View?
if (!myItemList[position].isHeader) {
rowView = inflater.inflate(R.layout.item_simple, parent, false)
rowView.itemTitle.text = myItemList[position].name
} else {
rowView = inflater.inflate(R.layout.item_header_group, parent, false)
rowView.header.text = myItemList[position].name
}
return rowView
}
override fun isEnabled(position: Int): Boolean {
return !myItemList[position].isHeader
}
}
在片段中,我有以下代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.viewModel.myListItem.observe(this, Observer<List<MyItem>> { item ->
val adapter = MyListAdapter(this.view!!.context, item!!)
this.view!!.listViewFragment.adapter = adapter
this.view!!.listViewFragment.divider = null
this.view!!.listViewFragment.isClickable = true
})
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_listview, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.validatePenChoiceButton.setOnClickListener {
viewModel.validate(this.view!!.listViewFragment.checkedItemPosition)
}
}
listview cml代码:
<ListView
android:id="@+id/listViewFragment"
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="30"
android:choiceMode="singleChoice"
android:listSelector="@color/item_listSelector_color"
android:textColor="@color/white" />
我尝试了以下方法:更改检查项目,但未突出显示列表视图。
this.view!!.listViewFragment.setItemChecked(3, true)
我已经对此进行了测试:
this.view!!.listViewFragment.refreshDrawableState()
this.view!!.listViewFragment.invalidateViews()
如何正确设置默认突出显示的项目?
答案 0 :(得分:1)
我创建了示例RecyclerView适配器。您可以使用View而不是TextView和setBackgroundColor()代替setTextColor()。您应该在片段中触发checkedItem(int)方法,然后触发notifyDataSetChanged或notifyItemChanged()
/**
* Created by beyazid on 11.03.2019.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<String> list;
private int indexOfHighlightedItem = -1;
public MyAdapter(Context context, ArrayList<String> list) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.my_row_for_recycler_view, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.bindItem(position);
holder.tvDummy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
indexOfHighlightedItem = position;
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void checkedItem(int pos){
indexOfHighlightedItem = pos;
}
}