【问题标题】:Getting a view's index in a LinearLayout在 LinearLayout 中获取视图的索引
【发布时间】:2020-10-15 09:34:00
【问题描述】:

所以我在我的 XML 文件中设置了一个 LinearLayout,并且我在我的活动启动时通过代码动态地添加了一堆 CardView。我怎样才能使当我点击任何 CardViews 时,我能够获得它的位置?

我尝试为 LinearLayout 设置 onClickListener,并为每张卡片设置 OnClickLIstener,但找不到获取被点击卡片索引的方法。

我真的很想知道在哪里放置onClickListener,以及如何获取点击的位置,以便我可以根据点击的内容开始新的活动。

这是我的代码:

private LinearLayout sLinearLayout;
private CardView[] cardViews;
private Workout[] workouts;
private ViewGroup.LayoutParams params;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.saved_workouts);
    Intent intent = getIntent();
    Parcelable[] parcelableArrayExtra = intent.getParcelableArrayExtra(MainActivity.EXTRA_WORKOUTS);
    workouts = new Workout[parcelableArrayExtra.length];
    System.arraycopy(parcelableArrayExtra, 0, workouts, 0, parcelableArrayExtra.length);
    sLinearLayout = findViewById(R.id.viewList);

    // Set the CardView layoutParams
    params = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
    );

    populateCardViews();
    populateLayout();

}

//TODO: this method will populate the linear layout, filling the list.
private void populateLayout() {

    for (CardView cardView : cardViews) {
        sLinearLayout.addView(cardView);
    }

}

//TODO: this method will fill up the CardViews array with an array of workouts.
private void populateCardViews() {
    cardViews = new CardView[workouts.length];
    for(int i = 0; i < workouts.length; i++) {
        CardView card = new CardView(this);
        card.setClickable(true);
        card.setLayoutParams(params);
        // Set CardView corner radius
        card.setRadius(9);
        // Set cardView content padding
        card.setContentPadding(15, 15, 15, 15);
        // Set a background color for CardView
        card.setCardBackgroundColor(Color.BLUE);
        // Set the CardView maximum elevation
        card.setMaxCardElevation(50);
        // Set CardView elevation
        card.setCardElevation(25);
        // Initialize a new TextView to put in CardView

        TextView tv = new TextView(this);
        tv.setLayoutParams(params);
        tv.setText("Workout WorkTime: " + workouts[i].getWorkTime());
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
        tv.setTextColor(Color.WHITE);

        card.setTag(workouts[i].getWorkTime());
        card.addView(tv);

        cardViews[i] = card;
    }
}

【问题讨论】:

    标签: java android xml android-linearlayout


    【解决方案1】:

    我认为您可以为每张卡片 setOnClickListener。点击卡片的索引为i

    for (int i = 0; i < workouts.length; i++) {
        ...
    
        final int finalI = i;
        card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("TAG", "You click at card: " + finalI);
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      我不好以编程方式添加这么多布局。您可以使用回收站视图和适配器来显示项目列表。你可以阅读:https://developer.android.com/guide/topics/ui/layout/recyclerview

      顺便说一句,在你的情况下:当你创建一个 Cardview 时,只需将onClickListener 添加到它,你的 Cardview 的索引就是它在 Cardview 列表中的索引。

      CardView cardView = new CardView(this);
      cardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              //handle click here
          }
      });
      

      【讨论】:

        【解决方案3】:

        看看这是否有效。

        cardview.setOnClickListener(new OnClickListener() {
        
                @Override
                public void onClick(View v) {
                 int index = linearLayout.indexOfChild(v);
        
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-21
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多