setVisibility属性布局的动态ID

时间:2016-09-20 11:21:13

标签: android listview visibility

我创建了ListView,其中每个按钮ID包含 database_idx10 + button_number

例如,我将id设置为101,即10 = database_id & 1 = button_number时

现在我要setVisibility101View.GONE android:onClick="click"这是我生成的唯一ID。

如何使用此生成的ID将可见性设置为true。

我通过调用用户定义函数“click”来重新获取此id,而在xml中我设置了public void click(View view) { final int position = view.getId(); int button_number = position % 10; int id = position/10; int layout_id=id*10+2; if(button_number==1){ //have to set visibity true of layout_id } }

ALTER TABLE your_table_name CHANGE user_id your_new_name INT;

注意

  

我能够设置从可见按钮到消失按钮的可见性,但不是   对面。

4 个答案:

答案 0 :(得分:0)

如果此按钮是您的视图对象的Child,您可以执行类似这样的操作

Button btn = (Button) view.findViewById(your_id);
btn.setVisibility(View.VISIBLE);

或者如果不是,你必须在他的父布局或活动中找到这个视图。

答案 1 :(得分:0)

这不是问题的解决方案,但无论如何唯一地识别列表项中的按钮很容易。您不必自己设置唯一ID。列表中每个按钮的View引用已经不同了。您只需使用position方法中的getView来识别它们即可。

以下是列表中适配器的示例getView方法。

public View getView (int position, View convertView, ViewGroup parent){
    if( convertView == null ){
        //We must create a View:
        convertView = inflater.inflate(R.layout.my_list_item, parent, false);
    }
    // Here we can do changes to the convertView
    Button b = convertView.findViewById(R.id.button);

    // Set your visibility here
    if(your_condition_is_true) b.setVisibility(View.VISIBLE);
    else b.setVisibility(View.GONE);

    return convertView;
}

答案 2 :(得分:0)

尝试这样做 view.setVisibility(View.VISIBLE);

答案 3 :(得分:0)

您需要boolean类中的Details变量来保持详细信息部分的可见性。

您的适配器代码应为,

class MyAdapter extends BaseAdapter {

    ArrayList<Details> list;
    Context context;

    public MyAdapter(Context context, RealmResults<Details> result2) {
        this.context = context;
        this.list = result2;
    }

    public class Holder {
        TextView topic;
        TextView detail;
        LinearLayout details;
        ImageButton send;
        ImageButton edit;
        ImageButton delete;

        public Holder(View v) {
            this.topic= (TextView) v.findViewById(R.id.TextView1);
            this.detail= (TextView) v.findViewById(R.id.td);
            this.details = (LinearLayout) v.findViewById(R.id.details);
            this.send= (ImageButton) v.findViewById(R.id.imagebutton1);
            this.edit= (ImageButton) v.findViewById(R.id.imagebutton3);
            this.delete= (ImageButton) v.findViewById(R.id.imagebutton2);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View row=view;
        Holder holder=null;
        if(row == null) {
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(MainActivity.context.LAYOUT_INFLATER_SERVICE);
            row= inflater.inflate(R.layout.layout,viewGroup,false);
            holder = new Holder(row);
            row.setTag(holder);
        } else {
            holder= (Holder) row.getTag();
        }

        final Details temp = (Details) getItem(i);

        holder.topic.setText(temp.getTopic());
        holder.detail.setText(temp.getTopic_details());

        if(temp.isDetailButtonVisible()){
            holder.details.setVisiblity(View.VISIBLE);
        } else {
            holder.details.setVisiblity(View.GONE);         
        }

        holder.topic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                temp.setDetailButtonVisibility(false);
            }
        });

        holder.send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        holder.detail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });



        holder.edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        holder.delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return row;
    }


}

你的布局应该是,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="6"
        android:orientation="horizontal"
        android:background="#5894CA"
        android:clickable="true">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:id="@+id/TextView1"
            android:text="Bank of baroda details...."
            android:layout_marginStart="10dp"
            android:textStyle="bold"
            android:fontFamily="sans-serif-condensed"
            android:layout_marginTop="10px"
            android:textSize="20dp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="horizontal">

            <ImageButton
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:id="@+id/imagebutton1"
                android:background="@drawable/share" />

            <ImageButton
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:id="@+id/imagebutton3"
                android:layout_marginStart="10dp"
                android:background="@drawable/edit_text" />

            <ImageButton
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:id="@+id/imagebutton2"
                android:layout_marginStart="10dp"
                android:background="@drawable/gnome_edit_delete" />

            </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:orientation="vertical"
        android:clickable="true"
        android:id="@+id/details"
        android:visibility="invisible" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="148dp"
            android:background="@drawable/bg"
            android:text="I have a LinearLayout that I've styled to look like a button, and it contains a few text/ImageView elements. I would like to make the whole LinearLayout act like a button, in particular to give it states that are defined in a so it has a different background when it is pressed..."
            android:layout_margin="10dp"
            android:id="@+id/td" />

    </LinearLayout>

</LinearLayout>