找到你要的答案

Q:Fragments, Cards, ListViews and Images

Q:碎片、卡、列表视图和图像

It has been a long time since I did some Android and now I want to get back to it. I still try to grasp the concept of using Fragments properly and what would be the proper way to do what I need to.

To the point. I need to have a Main view that (for now) contains one card. This card will contain a TextView and a ListView with custom items(one image and two rows of text). How would I implement this?

Do I need to do a different fragment for the listView and another for the card? I know I need a custom Adapter for the list view but how do I go on putting everything together?

Any advice is welcomed, even in the most basic diagram form.

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>

它已经很长一段时间以来,我做了一些Android,现在我想回到它。我仍然试图掌握使用片段的概念,什么是正确的方式来做什么,我需要。

说到要点。我需要有一个主视图(现在)包含一张卡。这张卡将包含一个TextView和自定义项目列表(一个图像和两行文本)。我将如何实现这一点?

我是否需要为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>
answer1: 回答1:

First of all, if you want to have different navigatable cards containing text and list views, I would recommend using a PageAdapter , in which each card can be loaded to the adapter in the beginning of the Activity like this:

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;

}

And for the List in your card, create a New subclass of Base Adapter Class and instantiate the layout my_listitem.xml there and return the view for each item in list , this can be done like this:

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;

}

}

You may need to set the List in the CustomListAdapter Class using some setter method and then in the getView method you can create the view for each item in the list Item using the list.

To use the CustomListAdapter as the adapter for your list in the card in CustomAdapter class add this:

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

Fragments are used for changing part of Main UI .Refer the Below link:http://developer.android.com/guide/components/fragments.html

首先,如果你想有不同的导航卡包含文本和列表视图,我会建议使用pageadapter,其中每个卡可以装在这样的活动开始的适配器:

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;

}

和在你的卡的列表,创建一个新的子类底座适配器类和实例化布局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方法,然后在getview方法可以创建视图为每个项目使用列表中的列表项的customlistadapter类集列表。

使用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

android  android-layout  listview  android-fragments