布局的动态 id 的 setVisibility true
setVisibility true of dynamic id of a layout
我创建了 ListView
,其中每个按钮 ID 包含 database_idx10+button_number
例如,我将id设置为101即10=database_id & 1=button_number
现在我必须将 ID 101
的 setVisibility
更改为 View.GONE
这是我生成的唯一 ID。
如何使用生成的 ID 将可见性设置为 true。
我正在通过调用用户定义函数 "click" 来检索此 ID,在 xml 中我设置了 android:onClick="click"
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
}
}
注意
I was able to set visibility from visible to gone button but not the
opposite.
如果此按钮是您的视图对象的子按钮,您可以这样做
Button btn = (Button) view.findViewById(your_id);
btn.setVisibility(View.VISIBLE);
或者,如果不是,则必须在他的父布局或 activity 中找到此视图。
这不是问题的解决方案,但无论如何在列表项中唯一标识按钮很容易。您实际上不必自己设置唯一 ID。列表中每个按钮的 View
引用已经不同。您只需在 getView
方法中使用 position
来识别它们。
这是列表中适配器的示例 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;
}
尝试这样做
view.setVisibility(View.VISIBLE);
您需要在 Details
class 中使用一个 boolean
变量来保持详细信息部分的可见性。
您的适配器代码应该是,
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>
我创建了 ListView
,其中每个按钮 ID 包含 database_idx10+button_number
例如,我将id设置为101即10=database_id & 1=button_number
现在我必须将 ID 101
的 setVisibility
更改为 View.GONE
这是我生成的唯一 ID。
如何使用生成的 ID 将可见性设置为 true。
我正在通过调用用户定义函数 "click" 来检索此 ID,在 xml 中我设置了 android:onClick="click"
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
}
}
注意
I was able to set visibility from visible to gone button but not the opposite.
如果此按钮是您的视图对象的子按钮,您可以这样做
Button btn = (Button) view.findViewById(your_id);
btn.setVisibility(View.VISIBLE);
或者,如果不是,则必须在他的父布局或 activity 中找到此视图。
这不是问题的解决方案,但无论如何在列表项中唯一标识按钮很容易。您实际上不必自己设置唯一 ID。列表中每个按钮的 View
引用已经不同。您只需在 getView
方法中使用 position
来识别它们。
这是列表中适配器的示例 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;
}
尝试这样做 view.setVisibility(View.VISIBLE);
您需要在 Details
class 中使用一个 boolean
变量来保持详细信息部分的可见性。
您的适配器代码应该是,
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>