在自定义列表视图适配器(kotlin)中设置默认突出显示的项目
Set default highlighted item in custom listview adapter (kotlin)
我想默认使用 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" />
我尝试了以下方法:checkitem 已更改,但 listview 未突出显示。
this.view!!.listViewFragment.setItemChecked(3, true)
我已经测试了这个:
this.view!!.listViewFragment.refreshDrawableState()
this.view!!.listViewFragment.invalidateViews()
如何正确设置默认高亮项?
我创建了示例 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;
}
}
我想默认使用 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" />
我尝试了以下方法:checkitem 已更改,但 listview 未突出显示。
this.view!!.listViewFragment.setItemChecked(3, true)
我已经测试了这个:
this.view!!.listViewFragment.refreshDrawableState()
this.view!!.listViewFragment.invalidateViews()
如何正确设置默认高亮项?
我创建了示例 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;
}
}