【问题标题】:Select listview item on second click在第二次单击时选择列表视图项目
【发布时间】:2016-09-28 09:25:49
【问题描述】:

代码使用 baseadapter 用项目填充列表视图。单击该项目时,它会更改背景颜色并转到下一个活动。现在我想在第一次单击时更改背景颜色,它应该保持选中状态。然后在下一次单击时,它应该转到下一个活动。有没有可能。

活动

    nameList = (ListView) findViewById(R.id.list_names);
    nameList.setAdapter(listAdapter);
    listAdapter.notifyDataSetChanged(); 

    nameList.setOnItemClickListener(new AdapterView.OnItemClickListener()
 {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) 
{

    TextView PCNtv = (TextView) view.findViewById(R.id.value_Name);
    String P_Name = PCNtv.getText().toString();

    Intent intents = new Intent(getApplicationContext(), Activity2.class);
    intents.putExtra("P_Name", P_Name);

    view.setSelected(true);                    
    startActivity(intents);
      }
    });

在可绘制中

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
   <item android:state_selected="true" android:drawable="@drawable/blue_color"/> 
   <item android:state_pressed="true" android:drawable="@drawable/white_color"/> 
   <item android:state_focused="true" android:drawable="@drawable/red_color"/> 
</selector>

【问题讨论】:

  • 取一个计数器作为int变量,点击后增加。如果计数器为 2,则开始活动。
  • 您要选择多个项目还是只选择一个项目(也就是选择新项目会导致取消选择前一个项目)?
  • @skywall 我只想选择一项。
  • @Bansal 我会努力做到的。
  • 我什至不会为柜台费心。每次单击该项目时,检查 isSelected(),如果它已被选中,则转到下一个活动。

标签: android listview


【解决方案1】:

使用views selected 属性来检测第二次点击。第一次单击将选择视图,第二次将打开活动。

nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) 
  {

      TextView PCNtv = (TextView) view.findViewById(R.id.value_Name);
      String P_Name = PCNtv.getText().toString();

      if ( view.isSelected() ) {
           Intent intents = new Intent(getApplicationContext(), Activity2.class);
           intents.putExtra("P_Name", P_Name);
           startActivity(intents);
      }
      view.setSelected(true);                    
    });
 }

更新

当用户再次按下视图时选择丢失,因此上面的代码不起作用。这是一个涉及适配器跟踪当前选定位置的解决方案。

点击事件:

 nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        // Check to see which item in the adapter is already
        // selected, if the same has been selected twice, start new activity
        if ( i == ((TestAdapter)adapter).getSelectedIndex()) {

           // Start your new activity here.
           Log.d("TAG", "second push");
        }

        ((TestAdapter)adapter).setSelectedIndex(i);
     }
  });

您没有显示适配器的代码,所以这里是一个添加了所选索引的简单代码:

public class TestAdapter extends ArrayAdapter<String>  {

      private final Activity context;
      private int mSelection = -1;

      public TestAdapter(Activity context, String[] objects) {
         super(context, R.layout.text_layout, objects);
         this.context = context;
      }

      // Call this when an item is clicked, this sets the 
      // internal index for the selected item and notifies the view
      // that the data has changes, this will force the view to draw
      // allowing it to correctly highlight the selected item

      public void setSelectedIndex(int index) {
         mSelection = index;
         notifyDataSetChanged();
      }

      // This is needed for the click listener to check to see which
      // item is already selected
      public int getSelectedIndex() {
         return mSelection;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater inflater = context.getLayoutInflater();
         View rowView = inflater.inflate(R.layout.text_layout, null);

         TextView tv = (TextView)rowView.findViewById(R.id.text3);
         tv.setText(getItem(position));

         // Adds or removes the selection on the view so
         // it will display as selected
         if ( mSelection == position ) {
            tv.setSelected(true);
         } else {
            tv.setSelected(false);
         }

         return rowView;
      }

   }

更新:添加内联 cmets

【讨论】:

  • 如果用户选择第 2 行,然后选择第 3 行,会有什么行为?第 2 行是失去选择还是它们都被选中?
  • 当我第一次点击时,项目会改变颜色。然后当我第二次单击它时,它会保持相同的活动。对于您的问题 - 是的,它失去了选择。
  • view.setSelected(true);后没有进入if条件;
  • 是的,再次触摸视图时会失去选择。我将用更复杂的解决方案更新我的答案,这也解决了多选问题。
  • 我应该将意图部分添加到 if 条件中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2018-12-17
相关资源
最近更新 更多