我已经创建了一个自定义列表视图,我想在列表视图中预先选择一个项目。我在网上尝试了很多事情,但是没有运气。下面给出了我的尝试。
lvModifierSet.requestFocusFromTouch();
lvModifierSet.setSelection(1);
lvModifierSet.setItemChecked(1, true);
lvModifierSet.performItemClick(lvModifierSet.getChildAt(1), 1, lvModifierSet.getItemIdAtPosition(1));
这是我的列表视图
<ListView
android:id="@+id/menu_lv_modifier_set"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="@color/colorWhite"
android:divider="@color/colorLightGray"
android:dividerHeight="1dp"
android:listSelector="@color/colorLightGray" />
自定义适配器代码
public class ModifierSetListAdapter extends ArrayAdapter<ModifierSet> {
private Context context;
private int resourceLayout;
private List<ModifierSet> modifierSets;
public ModifierSetListAdapter(Context context, int resource, List<ModifierSet> modifierSets) {
super(context, resource, modifierSets);
this.context = context;
resourceLayout = resource;
this.modifierSets = modifierSets;
}
@Nullable
@Override
public ModifierSet getItem(int position) {
return modifierSets.get(position);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = LayoutInflater.from(context);
view = vi.inflate(resourceLayout, null);
}
ModifierSet modifierSet = modifierSets.get(position);
view.setId(modifierSet.getMenu_modifier_set_id());
TextView tvModifierName = view.findViewById(R.id.modifier_set_tv_modifier_name);
tvModifierName.setText(modifierSet.getModifier_set_name());
return view;
}
}
答案 0 :(得分:1)
使用Runnable()
尝试类似的事情:
lvModifierSet.post(new Runnable() {
@Override
public void run() {
lvModifierSet.requestFocusFromTouch();
lvModifierSet.setSelection(1);
}
});
答案 1 :(得分:0)
您需要使用ArrayList
进行操作。在模型类中添加isSelected
布尔参数,如下所示:-
private boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setIsSelected(boolean isSelected) {
isSelected = isSelected;
}
在您的 Java
类中,我们需要分配特定的项目(已选择或未选择)。
YOUR_ARRAYLIST.get(POSITION_WHICH_U_WANT_TO_SELECT).setIsSelected(true);
在适配器类的getView
方法中,您需要检查视图是否为 SELECTED 或 UN-SELECTED
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ModifierSet dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
if(dataModel.get(position).isSelected())
{
/**
* SET THE LISTVIEW'S ITEM SELECTED HERE
*/
}
else
{
/**
* SET THE LISTVIEW'S ITEM UN-SELECTED HERE
*/
}
}