我创建了android应用程序,我希望使用自定义适配器隐藏和显示ListView中的视图。
我的代码是:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.item_estimate, null);
btn_dots = (ImageButton) vi.findViewById(R.id.btn_dots_itemEstimate);
ll_2 = (LinearLayout) vi.findViewById(R.id.ll_2_itemEstimate);
}
btn_dots.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
if (ll_2.getVisibility() == View.GONE)
{
ll_2.setVisibility(View.VISIBLE);
}
else
{
ll_2.setVisibility(View.GONE);
}
}
});
return vi;
}
点击Dots时,它不会隐藏布局。
感谢。
答案 0 :(得分:3)
是的,我遇到了同样的问题并使用ExpandableListView解决了它。我正在粘贴下面的代码
public DefaultContactListAdapter(Context context, ArrayList<Cont> List2) {
super();
this.context = context;
this.contactList = contactList2;
@Override
public Object getChild(int parent, int child) {
return child;
// return glossariesList.get(Sub_List.get(parent) ).get(child);
}
@Override
public long getChildId(int parent, int child) {
// TODO Auto-generated method stub
return child;
}
@Override
public View getChildView(int parent, int child,
boolean lastChild, View convertview, ViewGroup parentview)
{
// String child_title=(String) getChild(parent, child);
if(convertview==null)
{
LayoutInflater inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertview=inflator.inflate(R.layout.layout_default_contact_screen_child, parentview, false);
}
return convertview;
}
@Override
public int getChildrenCount(int groupPosition)
{
return 1;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return contactList.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return contactList.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int parent, boolean isExpanded,
View convertView, ViewGroup parentview) {
// TODO Auto-generated method stub
//String group_title=(String) getGroup(parent);
if(convertView==null)
{
LayoutInflater inflator=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView=inflator.inflate(R.layout.layout_default_screen, parentview,false);
}
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
答案 1 :(得分:1)
尝试Expandable listview。 此链接可以帮助您
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
答案 2 :(得分:0)
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class First_Activity extends Activity
{
ListView listView;
ArrayList<Item> arrayList= new ArrayList<Item>();
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_actiivty);
listView =(ListView) findViewById(R.id.listView);
Item item = new Item();
item.setText1("1");
item.setText2("2");
item.setSelected(false);
arrayList.add(item);
Item item1 = new Item();
item1.setText1("1");
item1.setText2("2");
item1.setSelected(true);
arrayList.add(item1);
Item item2 = new Item();
item2.setText1("1");
item2.setText2("2");
item2.setSelected(true);
arrayList.add(item2);
adapter = new Adapter();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
//Toast.makeText(getApplicationContext(), ""+position, 1).show();
for (Item item :arrayList){
item.setSelected(false);
}
arrayList.get(position).setSelected(true);
System.out.println(arrayList.get(0).isSelected());
System.out.println(arrayList.get(1).isSelected());
System.out.println(arrayList.get(2).isSelected());
adapter.notifyDataSetChanged();
}
});
}
public class Adapter extends BaseAdapter
{
public Adapter()
{
}
@Override
public int getCount()
{
return arrayList.size();
}
@Override
public Object getItem(int position)
{
return arrayList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent, false);
holder.text1 = (TextView) convertView.findViewById(R.id.textView1);
holder.text2 = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(arrayList.get(position).getText1());
holder.text2.setText(arrayList.get(position).getText2());
if(arrayList.get(position).isSelected()==true)
{
holder.text2.setVisibility(View.VISIBLE);
}
else
{
holder.text2.setVisibility(View.GONE);
}
return convertView;
}
class ViewHolder
{
TextView text1;
TextView text2;
}
}
}
javabeen get set
public class Item
{
private String text1;
private String text2;
private boolean isSelected;
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
主XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
行XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 3 :(得分:0)
使用此
覆盖您的点击监听器btn_dots.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
if (arg0.getVisibility() == View.GONE)
{
arg0.setVisibility(View.VISIBLE);
}
else
{
arg0.setVisibility(View.GONE);
}
}
});
答案 4 :(得分:-2)
您可以在一行代码中执行此操作:
btn_dots.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
arg0.setVisibility(arg0.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
});