Android:具有下拉详细视图的列表视图(不是微调器或不可扩展列表视图)

时间:2015-10-06 16:22:51

标签: android android-listview drop-down-menu

您好我正在尝试使用下拉详细视图执行列表视图。例如,请参阅此Image 1。当我们点击任何列表视图时,它会显示详细视图(相对于列表视图),如Image 2。此详细视图不像可扩展列表视图或微调器。请帮我这样做

2 个答案:

答案 0 :(得分:2)

请结帐此github,我只需设置https://github.com/lt-tibs1984/ExpandableListView

这个想法是隐藏并使用可见性显示视图,这里有一些代码说明了这一点:

首先设置你的Item类或你想要使用的任何对象(简单的pojo实现Parcelable)

public class Item implements Parcelable{

public String title;
public String description;
public boolean isExpanded;

public Item(){}

public Item(Parcel in){
    title = in.readString();
    description = in.readString();
    isExpanded = in.readInt() == 1;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(description);
    dest.writeInt(isExpanded ? 1 : 0);
}

public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>(){
    @Override
    public Item createFromParcel(Parcel source) {
        return new Item(source);
    }

    @Override
    public Item[] newArray(int size) {
        return new Item[size];
    }
};
}

然后创建你的适配器:

public class ExpandableAdapter extends BaseAdapter{

List<Item> items;
Context context;

public class Row{
    AppCompatTextView mTvTitle;
    AppCompatTextView mTvDescription;
    FrameLayout mFlWrapper;
    ImageView mIvArrow;
}

public ExpandableAdapter(Context context, List<Item> items){
    this.items = items;
    this.context = context;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Row theRow;

    if(convertView == null){
        theRow = new Row();
        convertView = LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
        theRow.mFlWrapper = (FrameLayout) convertView.findViewById(R.id.fl_wrapper);
        theRow.mTvTitle = (AppCompatTextView) convertView.findViewById(R.id.tv_title);
        theRow.mTvDescription = (AppCompatTextView) convertView.findViewById(R.id.tv_description);
        theRow.mIvArrow = (ImageView) convertView.findViewById(R.id.iv_arrow);

        convertView.setTag(theRow);
    }else{

        theRow = (Row) convertView.getTag();
    }

    // Update the View
    Item item = items.get(position);
    if(item.isExpanded){
        theRow.mFlWrapper.setVisibility(View.VISIBLE);
        theRow.mIvArrow.setRotation(180f);
    }else{
        theRow.mFlWrapper.setVisibility(View.GONE);
        theRow.mIvArrow.setRotation(0f);
    }

    theRow.mTvTitle.setText(item.title);
    theRow.mTvDescription.setText(item.description);


    // return the view
    return convertView;
}
}

最后在你的活动中:

 public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ListView lvItems = (ListView) findViewById(R.id.lv_items);
    ExpandableAdapter adapter = getAdapter();

    lvItems.setAdapter(adapter);
    lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ExpandableAdapter adapter = (ExpandableAdapter) parent.getAdapter();

            Item item = (Item) adapter.getItem(position);
            if(item != null){
                if(item.isExpanded){
                    item.isExpanded = false;

                }else{
                    item.isExpanded = true;
                }
            }
            adapter.notifyDataSetChanged();
        }
    });
}


private ExpandableAdapter getAdapter(){

    List<Item> items = new ArrayList<>();

    for(int i = 0; i < 50; i++){
        Item item = new Item();
        item.title = "Title Item " + i;
        item.description = "Description for Title Item "+ i;
        item.isExpanded = false;

        items.add(item);
    }

    return new ExpandableAdapter(this, items);
}
}   

答案 1 :(得分:0)

特别感谢&#34; inner_class7&#34;:根据&#34; inner_class7&#34;我得到了答案,但通过使用他的代码我必须达到两个要求

要求1:当我们点击说明时,展开的视图正在关闭,但我不想关闭视图

要求2:我希望在扩展新视图时关闭上一个视图(如果存在)

要达到以上两个要求,请更新&#34; inner_class7&#34;代码根据以下步骤

第1步:创建 Global.java

public class Global {

  public static Item itemGlobal;

}

第2步:更新 MainActivity.java

import static com.app.listviewdrop.Global.itemGlobal;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view_drop);


    ListView lvItems = (ListView) findViewById(R.id.lv_items);
    ExpandableAdapter adapter = getAdapter();

    lvItems.setAdapter(adapter);
    lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ExpandableAdapter adapter = (ExpandableAdapter) parent.getAdapter();

            Item item = (Item) adapter.getItem(position);

            if(item != null){
                if(item.isExpanded){
                    item.isExpanded = false;

                }else{
                    item.isExpanded = true;

                    itemGlobal = (Item)adapter.getItem(position);
                }
            }

            adapter.notifyDataSetChanged();
        }
    });
}


private ExpandableAdapter getAdapter(){

    List<Item> items = new ArrayList<Item>();

    for(int i = 0; i < 50; i++){
        Item item = new Item();
        item.title = "Title Item " + i;
        item.description = "Description for Title Item "+ i;
        item.isExpanded = false;

        items.add(item);
    }

    return new ExpandableAdapter(this, items);
}
}

第3步:更新 ExpandableAdapter.java

import static com.YOUR PACKAGE NAME.itemGlobal;


public class ExpandableAdapter extends BaseAdapter {

List<Item> items;
Context context;
// List<Item> list = new ArrayList<Item>();


public class Row {
    AppCompatTextView mTvTitle;
    AppCompatTextView mTvDescription;
    FrameLayout mFlWrapper;
    ImageView mIvArrow;
}

public ExpandableAdapter(Context context, List<Item> items) {
    this.items = items;
    this.context = context;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Row theRow;

    if (convertView == null) {
        theRow = new Row();
        convertView = LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
        theRow.mFlWrapper = (FrameLayout) convertView.findViewById(R.id.fl_wrapper);
        theRow.mTvTitle = (AppCompatTextView) convertView.findViewById(R.id.tv_title);
        theRow.mTvDescription = (AppCompatTextView) convertView.findViewById(R.id.tv_description);
        theRow.mIvArrow = (ImageView) convertView.findViewById(R.id.iv_arrow);

        convertView.setTag(theRow);


        theRow.mFlWrapper.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });


    } else {

        theRow = (Row) convertView.getTag();
    }

    // Update the View
    Item item = items.get(position);


    if (item.isExpanded) {

        if(item == itemGlobal){

            theRow.mFlWrapper.setVisibility(View.VISIBLE);
            theRow.mIvArrow.setRotation(180f);

        }else {
            theRow.mFlWrapper.setVisibility(View.GONE);
            theRow.mIvArrow.setRotation(0f);

            item.isExpanded = false;
        }

    } else {
        theRow.mFlWrapper.setVisibility(View.GONE);
        theRow.mIvArrow.setRotation(0f);


    }


    theRow.mTvTitle.setText(item.title);
    theRow.mTvDescription.setText(item.description);

    // return the view
    return convertView;
}

}

我仅通过&#34; inner_class7&#34;的帮助实现了这些要求。很多人都要感谢&#34; inner_class7&#34;