代码之家  ›  专栏  ›  技术社区  ›  byteC0de

如何预选listview行?

  •  1
  • byteC0de  · 技术社区  · 6 年前

    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;
    
        }
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   ʍѳђઽ૯ท    6 年前

    用这个试试 Runnable() :

    lvModifierSet.post(new Runnable() {
    
            @Override
            public void run() {
                lvModifierSet.requestFocusFromTouch();
                lvModifierSet.setSelection(1);
            }
        });
    
        2
  •  0
  •   Ravindra Kushwaha    6 年前

    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 方法,你需要检查视图是否正确 挑选出来的 未选定

      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
                 */
              }  
          }