Android ListView显示一些项目和显示更多按钮

时间:2016-04-28 16:03:54

标签: java android listview

我发现很难找到我正在寻找的东西所以我想我会尝试在这里解释一下。我目前在ScrollView中有一个自定义ListView。每个ListView项目包含商店的分支详细信息,即街道地址,城市,电话。我已经使用下面显示的setListViewHeightBasedOnChildren函数来解决“在ScrollView中滚动ListView”问题,将ListView扩展到它的全高。

我现在要做的是,如果商店有超过2个分支,则显示2个分支,然后在列表视图底部显示“显示更多”按钮。如果可能的话,按钮将位于列表的顶部,部分显示第三个分支,为其提供类似于this的透明渐变效果。当向下打开ListView时,单击该按钮将理想地为ListView设置动画。我知道这是一个很大的问题,但我想知道它是否可能并且正确的方向指出了一点。一旦我知道从哪里开始,我就可以自己开发自定义解决方案。

我的ListView实现和布局文件如下所示:

BranchListAdapter.java

public class BranchListAdapter extends ArrayAdapter<HashMap<String, String>> {

private Context mContext;
private LayoutInflater mLayoutInflater;

public BranchListAdapter(Context context) {
    super(context, android.R.layout.simple_list_item_1);
    mContext = context;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

    if(convertView == null) {
        view = mLayoutInflater.inflate(R.layout.fragment_shop_details_branch_list_item, parent, false);
    } else {
        view = convertView;
    }

    final HashMap<String, String> branch = getItem(position);

    TextView address = (TextView) view.findViewById(R.id.branch_list_item_address);
    TextView city = (TextView) view.findViewById(R.id.branch_list_item_city);
    TextView phone = (TextView) view.findViewById(R.id.branch_list_item_phone);

    address.setText(branch.get("address"));
    city.setText(branch.get("city"));
    phone.setText(branch.get("phone"));

    return view;
}

public void setData(List<HashMap<String, String>> branches) {
    clear();
    if(branches != null) {
        for(HashMap<String, String> branch : branches) {
            if(!branch.get("address").equals(""))
                add(branch);
        }
    }
}
}

fragment_shop_details_branch_list_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
    android:id="@+id/branch_list_item_grid"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    grid:columnCount="2"
    grid:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp"
    xmlns:tools="http://schemas.android.com/tools">

<TextView
    android:text="Address"
    android:textColor="#000000"
    android:textSize="16sp"
    grid:layout_column="0"
    grid:layout_gravity="center_vertical"
    grid:layout_row="0"
    />

<TextView
    android:id="@+id/branch_list_item_address"
    android:layout_width="0dp"
    android:gravity="center"
    android:maxLines="2"
    tools:text="31 Fitzwilliam Manor, The Island"
    android:textColor="#000000"
    android:textSize="16sp"
    grid:layout_column="1"
    grid:layout_gravity="fill_horizontal"
    grid:layout_row="0"
    />

<TextView
    android:text="City"
    android:textColor="#000000"
    android:textSize="16sp"
    grid:layout_column="0"
    grid:layout_gravity="center_vertical"
    grid:layout_row="1"
    />

<TextView
    android:id="@+id/branch_list_item_city"
    android:layout_width="0dp"
    android:gravity="center"
    android:maxLines="2"
    tools:text="Dublin"
    android:textColor="#000000"
    android:textSize="16sp"
    grid:layout_column="1"
    grid:layout_gravity="fill_horizontal"
    grid:layout_row="1"
    />

<TextView
    android:text="Phone"
    android:textColor="#000000"
    android:textSize="16sp"
    grid:layout_column="0"
    grid:layout_gravity="center_vertical"
    />

<TextView
    android:id="@+id/branch_list_item_phone"
    android:layout_width="0dp"
    android:gravity="center"
    android:maxLines="2"
    tools:text="01 456 7890"
    android:textColor="#000000"
    android:textSize="16sp"
    grid:layout_column="1"
    grid:layout_gravity="fill_horizontal"
    grid:layout_row="2"
    />

<Button
    android:id="@+id/branch_list_item_button_showmap"
    style="?android:attr/borderlessButtonStyle"
    android:text="@string/show_on_map"
    android:textColor="#0645AD"
    android:textSize="14sp"
    grid:layout_column="0"
    grid:layout_columnSpan="2"
    grid:layout_columnWeight="1"
    grid:layout_gravity="center"
    grid:layout_row="4"/>
</android.support.v7.widget.GridLayout>

将列表视图展开为完整高度功能

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

最后,这就是ListView在Fragment中的实现方式:

    mBranchListAdapter = new BranchListAdapter(getActivity());
    mBranchListAdapter.setData(mShop.getBranchDetails());
    mBranchListView.setAdapter(mBranchListAdapter);
    MiscUtils.setListViewHeightBasedOnChildren(mBranchListView);

0 个答案:

没有答案