列表视图中的多选

Posted

技术标签:

【中文标题】列表视图中的多选【英文标题】:Multi selection in listview 【发布时间】:2016-12-31 09:14:13 【问题描述】:

我已经使用数组适配器创建了简单的多选列表视图。我想设置背景颜色特定的列表项。但是假设我选择了 2 项意味着设置背景,并且还设置了第 12 个位置和第 22 个位置,就像自动选择的那样。请提出问题。

黑色代码。

public class MainActivity extends Activity 

ListView lvCountry;

ArrayList<Integer> list = new ArrayList<Integer>();
String[] country =  "India", "USA", "Russsia", "China", "Pakistan",
        "Canada", "UK", "arcot", "vellore", "gudiyattam", "arani",
        "palani", "chennai", "padi", "velacherry", "ambattur",
        "ambatttur ot", "maduravoyal", "guindy" ;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lvCountry = (ListView) findViewById(R.id.listView1);
    // Array adapter
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            MainActivity.this, android.R.layout.simple_list_item_1, country);
    lvCountry.setAdapter(arrayAdapter);

    // list selection part
    lvCountry.setOnItemClickListener(new OnItemClickListener() 

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) 
            // color selection select item
            arg1.setBackgroundColor(Color.GRAY);
        
    );

【问题讨论】:

你必须在你的列表视图适配器中这样做 通过这个它可以帮助你mysamplecode.com/2012/07/android-listview-checkbox-example.html ~arg1.setBackgroundColor(Color.GRAY);~ 当项目设置 bgcolor 他们选择多项目请帮助解决问题并建议我 【参考方案1】:

实际上,当您滑动列表视图时,项目正在随机播放。为避免此问题,请将列表视图放在滚动视图中。如果您想要代码示例,我可以提供。

【讨论】:

检查下面的post xml文件和javafile 检查下面的post xml文件和javafile 检查下面的post xml文件和javafile【参考方案2】:

创建您自己的适配器,其中每个项目都有“已选择”标志,默认清除。在单击侦听器中为给定项目设置标志并强制重绘。你应该使用 RecuclerView,因为它工作得更快。当视图膨胀时,检查选择标志并相应地设置背景

【讨论】:

你没有回答他的问题【参考方案3】:

XML 文件

      <ScrollView
          android:layout_
    android:layout_
    android:layout_marginBottom="5dp"
    android:scrollbars="none"
    android:visibility="gone">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <ListView

            android:layout_
            android:layout_
            android:layout_margin="1dp"
            android:scrollbars="none"
            android:visibility="visible" />
    </LinearLayout>

</ScrollView>

java类

    listview.setAdapter(agentRequestAdapter);

    setListViewHeightBasedOnChildren(lvAgntRqstView);
         ////////////////////////////

    private static void setListViewHeightBasedOnChildren(ListView myListView) 
    try 
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) 
            //do nothing return null
            return;
        
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) 
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        
        //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));


     catch (NullPointerException e) 

    

【讨论】:

【参考方案4】:

在 bhatt4982 的这个答案中,您可以设置属性以使该选项可用于选择列表中的多个条目:

首先你可以像这个例子一样在 listView 本身中设置

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setItemCanFocus(false);

其次,您可以在布局或 layout_simple_list_item_1.xml 中声明它。 示例:

    <ListView
    ...
    android:background="@drawable/your_drawable"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>

然后你可以在你的自定义drawable中设置背景。

Source

【讨论】:

arg1.setBackgroundColor(Color.GRAY);//此代码实现,当特定项目点击设置bgcolor并自动选择另一个项目。请帮助 您好先生,我有一个关于屏幕上列表视图中的多选的查询我在列表视图中添加了 20 个项目,但在当前屏幕上只有 7 个项目当我滚动列表视图时它会跟随出现现在我的查询是当我在当前屏幕上多选 2、3、6 之类的项目时,但在第二页(向下滚动)之后的相同时间,在我不知情的情况下选择了这些项目(例如:屏幕 1 有 7 个项目,但我的选择只是2,3,6 仅此而已,但屏幕 2(向下滚动时)选择了 9,10,13 我不知道为什么在屏幕 2 上选择了该项目)你如何解决这个问题?【参考方案5】:
public class MainActivity extends AppCompatActivity 

List<Items> data;
RecyclerView rv;
ActionMode actionMode;
ListAdapterr listAdapterr;
int countSelected;
int total;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    CallAPI();

    rv.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), rv, new RecyclerItemClickListener.OnItemClickListener() 
        @Override
        public void onItemClick(View view, int position) 

            if (countSelected > 0)
            
                if (actionMode == null)
                
                    actionMode = MainActivity.this.startSupportActionMode(call);
                
                toggleSelection(position);
                String s = countSelected + " " + "S" + " " + total ;
                actionMode.setTitle(s);

                if (countSelected == 0)
                
                    actionMode.finish();
                
            else if (countSelected == 0)
            
                Toast.makeText(MainActivity.this, "Ok", Toast.LENGTH_SHORT).show();
            
        

        @Override
        public void onItemLongClick(View view, int position) 

            if (actionMode == null)
            
                actionMode = MainActivity.this.startSupportActionMode(call);
            
            toggleSelection(position);
            String s = countSelected + " " + "S" + " " + total ;
            actionMode.setTitle(s);

            if (countSelected == 0)
            
                actionMode.finish();
            
        
    ));



ActionMode.Callback call = new ActionMode.Callback() 
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) 
        mode.getMenuInflater().inflate(R.menu.menu_item,menu);
        actionMode = mode;
        countSelected = 0;
        return true;
    

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) 
        return false;
    

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) 
        return false;
    

    @Override
    public void onDestroyActionMode(ActionMode mode) 
        if (countSelected > 0) 
            deselectAll();
        
        actionMode = null;
    
;

ArrayList<Items> selectedList = new ArrayList<>();

private void toggleSelection(int position) 
    data.get(position).isSelected = !data.get(position).isSelected;

    if (data.get(position).isSelected) 
        selectedList.add(data.get(position));
        countSelected++;
        total +=Integer.valueOf(data.get(position).getAmount());
     else 
        selectedList.remove(data.get(position));
        countSelected--;
        total -= Integer.valueOf(data.get(position).getAmount());
    
    listAdapterr.notifyDataSetChanged();


private void deselectAll() 
    for (int i = 0, l = data.size(); i < l; i++) 
        data.get(i).isSelected = false;
    
    countSelected = 0;
    listAdapterr.notifyDataSetChanged();

回收站视图:

   public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener 
public interface OnItemClickListener 
    void onItemClick(View view, int position);
    void onItemLongClick(View view, int position);


private OnItemClickListener mListener;
private GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) 
    mListener = listener;

    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() 
        @Override
        public boolean onSingleTapUp(MotionEvent e) 
            return true;
        

        @Override
        public void onLongPress(MotionEvent e) 
            View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());

            if (childView != null && mListener != null) 
                mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
            
        
    );


@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) 
    View childView = view.findChildViewUnder(e.getX(), e.getY());

    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) 
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    

    return false;


@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) 


@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) 


【讨论】:

以上是关于列表视图中的多选的主要内容,如果未能解决你的问题,请参考以下文章

使用背景更改的多选列表视图

使用游标适配器实现带有过滤器的多选列表视图

Android 使用按钮和对话框复选框的多选项目,并在列表视图中显示所选项目而不是按钮

TableView Swift 中的多选

索引iCarousel中UIView的多选

我试图用列表中的对象填充列表视图,但只显示名称属性,同时允许用户在列表视图中多选项目