【问题标题】:Manage buttons in ListView to show/hide a part管理 ListView 中的按钮以显示/隐藏部分
【发布时间】:2023-04-10 23:16:01
【问题描述】:

我有一个ListViewButton。我想隐藏当我从第二个项目中单击 Button 时可见的项目。

如果我点击按钮“1”,我想隐藏蓝色的“布局”。如果单击是相同的项目,我可以管理布局,但是当我单击另一个项目的按钮时,布局仍然可见。如何设置这些项目的可见性?

这是可能的代码。 我的列表适配器

public View getView(int position, View convertView, ViewGroup parent) {
    final String item = items.get(position);
    View v = null;
    if (convertView != null)
        v = convertView;
    else
        v = inflater.inflate(R.layout.item, parent, false);
    TextView itemTV = (TextView) v.findViewById(R.id.item);
    itemTV.setText(item);
final ImageView number1 = (ImageView) v.findViewById(R.id.number1);

    final ImageView number2 = (ImageView) v.findViewById(R.id.number2);

    final ImageView button1 = (ImageView) v.findViewById(R.id.button1);

    final ImageView button2 = (ImageView) v.findViewById(R.id.button2);

    final RelativeLayout blue = (RelativeLayout) v.findViewById(R.id.blue);
    final RelativeLayout green = (RelativeLayout) v
            .findViewById(R.id.green);
    blue.setVisibility(RelativeLayout.GONE);
    green.setVisibility(RelativeLayout.GONE);



    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            if (on == 0) //show hide ---int on=0;
            {

                blue.setVisibility(RelativeLayout.VISIBLE);
                green.setVisibility(RelativeLayout.GONE);
                on = 1;

                Toast.makeText(context, "ImageButton clicked" + item,
                        Toast.LENGTH_SHORT).show();
                number1.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        Toast.makeText(context,
                                "CLICK" + item,
                                Toast.LENGTH_SHORT).show();

                    }
                });

                number2.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        Toast.makeText(context,
                                "ClICK" + item,
                                Toast.LENGTH_SHORT).show();

                    }
                });

            }// on equals 0
            else {
                on = 0;
                blue.setVisibility(RelativeLayout.GONE);
                green.setVisibility(RelativeLayout.GONE);
            }

        }
    });
    button2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            blue.setVisibility(RelativeLayout.GONE);
            green.setVisibility(RelativeLayout.VISIBLE);
            Toast.makeText(context, "CLICK" + item,
                    Toast.LENGTH_SHORT).show();
            number2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Toast.makeText(context, "CLICK" + item,
                            Toast.LENGTH_SHORT).show();

                }
            });

        }
    });

    return v;
}

我的活动

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    ListView lv = (ListView) findViewById( R.id.list );

    lv.setOnItemClickListener(
            new OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0,
                        android.view.View arg1, int arg2, long arg3) {



                    Toast.makeText( ListImageButton.this,
                            "List item clicked",
                            Toast.LENGTH_SHORT).show();
                }
        });
    ArrayList<String> items = new ArrayList<String>();
    items.add( "item1");
    items.add( "item2");
    items.add( "item3");
    ListAdapter adapter = new ListAdapter( this, items);
    lv.setAdapter( adapter );
}

【问题讨论】:

  • 您的问题含糊不清,无法得到直接的答案(我指的是但是当我单击另一个项目的按钮时,布局仍然可见)。基本上你需要一个自定义适配器,但没有其他细节很难推荐其他任何东西。也许发布您当前适配器的代码。
  • 当您连续单击1 按钮时,您是否希望:a) 隐藏该行的蓝色布局? b)隐藏所有行的所有蓝色布局? c) 别的东西? getView 方法中 on 变量的用途是什么。
  • 是的,当我单击按钮 1 或 2 表单 row-item2 时,我想隐藏 row-item1 下面可见的布局,例如。我的想法是当我单击按钮时只有一个子布局激活(如果我点击 button2 绿色布局,如果我点击 button1 蓝色布局)并隐藏另一个 item2 的子布局。
  • 如果你能提供更多关于你想在 ListView 中显示的数据的信息,我可以相应地建议你。
  • 我改变了我的形象,也许你现在会明白了。 @PareshMayani

标签: android android-layout android-listview android-button


【解决方案1】:

我假设您希望根据从一行中单击的数字按钮在 单个 行上显示蓝色或绿色部分。试试这样的:

// two fields in the adapter class
private int mBlueFlag = -1;
private int mGreenFlag = -1;


public View getView(int position, View convertView, ViewGroup parent) {
    final String item = items.get(position);
    View v = null;
    if (convertView != null) {
        v = convertView;
    } else {
        v = inflater.inflate(R.layout.item, parent, false);
    } 
    TextView itemTV = (TextView) v.findViewById(R.id.item);
    itemTV.setText(item);
    final ImageView number1 = (ImageView) v.findViewById(R.id.number1);
    final ImageView number2 = (ImageView) v.findViewById(R.id.number2);
    // I assume this are the buttons with 1 and 2 which show the green/blue row part
    final ImageView button1 = (ImageView) v.findViewById(R.id.button1);
    final ImageView button2 = (ImageView) v.findViewById(R.id.button2);
    final RelativeLayout blue = (RelativeLayout) v.findViewById(R.id.blue);
    final RelativeLayout green = (RelativeLayout) v.findViewById(R.id.green);
    if (mBlueFlag != -1 && mBlueFlag == position) {
    blue.setVisibility(View.VISIBLE);
    } else {
    blue.setVisibility(View.GONE);
    }
    if (mGreenFlag != -1 && mGreenFlag == position) {
    green.setVisibility(View.VISIBLE);
    } else {
        green.setVisibility(View.GONE);
    }

    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {          
             mGreenFlag = position;
             mBlueFlag = -1;
         // set the other OnCLickListener
         notifyDataSetChanged();
        }
    });
    button2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
             mBlueFlag = position;
         // set the other OnClickListeners
         notifyDataSetChanged();
        }
    });
    return v;
}

【讨论】:

    【解决方案2】:

    您需要更改基于此列表的信息,并调用notifityDataSetChanged(),它将重新创建列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 2019-08-23
      • 2012-10-12
      • 1970-01-01
      • 2016-03-30
      • 2017-12-30
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多