片段,卡片,列表视图和图像

时间:2015-05-04 16:11:55

标签: android android-layout listview android-fragments

自从我做了一些Android以来已经很长时间了,现在我想回到它。我仍然试图理解正确使用Fragments的概念以及正确的方法我需要什么。

重点。我需要一个Main视图(现在)包含一张卡片。此卡将包含TextView和带有自定义项的ListView(一个图像和两行文本)。我该如何实现呢?

我是否需要为listView执行不同的片段,为卡片执行另一个片段?我知道我需要一个自定义适配器用于列表视图但是如何将所有内容放在一起?

即使是最基本的图表形式,也欢迎任何建议。

fragment_main.xml

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity$PlaceholderFragment"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:paddingRight="5dp"
    android:paddingBottom="10dp">


    <android.support.v7.widget.CardView
        android:id="@+id/accounts_card"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_gravity="top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="150dp"
        android:layout_marginLeft="5dp"
        card_view:cardCornerRadius="0dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardElevation="3dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/accounts_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                style="@style/Base.TextAppearance.AppCompat.Caption"
                android:elevation="1dp"
                android:text="My Accounts"
                android:gravity="top|center"
                android:textStyle="bold"
                android:textSize="20dp"
                android:paddingTop="15dp"
                android:paddingBottom="15dp" />

            <ListView
                android:id="@+id/accounts_list"
                android:layout_below="@id/accounts_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

my_listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/account_item">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b_logo"
        android:elevation="1dp"/>

    <LinearLayout
        android:layout_alignEnd="@id/b_logo"
        android:paddingLeft="2dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right">
        <TextView
            android:id="@+id/account_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9959593922923"/>
        <TextView
            android:id="@+id/account_code"
            android:layout_below="@id/account_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="30-15-55"/>
    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

首先,如果你想拥有包含文本和列表视图的不同导航卡,我建议使用PageAdapter,其中每张卡都可以在Activity的开头加载到适配器,如下所示:

public class CustomAdapter extends PagerAdapter {

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    mContext=collection.getContext();
    LayoutInflater inflator=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view=inflator.inflate(R.layout.fragment_main, null);
    //do things to initiate the card like loading a text by accessing the text through TextView tv=(TextView)view.findElementsById(R.id.accounts_title);
    return view;

}

对于卡片中的List,创建一个Base Adapter Class的New子类并在那里实例化my_listitem.xml布局并返回列表中每个项目的视图,这可以这样做:

public class CustomListAdapter extends BaseAdapter {
private static List<?> list;
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
            RelativeLayout layout = null;
            salahLayout=(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.my_listitem, null);
            notifyDataSetChanged();// to notify the list has been changed
            return layout;

}

}

您可能需要使用某些setter方法在CustomListAdapter类中设置List,然后在getView方法中,您可以使用列表为列表Item中的每个项创建视图。

要使用CustomListAdapter作为CustomAdapter类卡中列表的适配器,请添加以下内容:

ListView list=(ListView)view.findElementById(R.id.accounts_list);
list.setAdapter(mCustomListAdapter);//set the object of the customlistadapter class

片段用于更改主界面的一部分。请参阅以下链接:http://developer.android.com/guide/components/fragments.html