首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Andorid Customize ListView做了几行特别的事

Andorid Customize ListView做了几行特别的事
EN

Stack Overflow用户
提问于 2016-02-29 07:56:21
回答 1查看 39关注 0票数 0

我有一个DrawerLayout,我的DrawerLayout当前显示一个包含图像和文本的列表视图,一切都很正常

现在,我决定在DrawerLayout中添加一个标题,并为列表视图中的几行添加一个特殊字段

谢谢

这张图片告诉了我想要做的一切:

http://8pic.ir/images/als4ye1elyo35tpju7hq.png

activity_main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/MainDrawerFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView android:id="@+id/MainDrawerListView"
    android:layout_width="270dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:divider="@null"
    android:background="@color/DrawerBackGround" />

</android.support.v4.widget.DrawerLayout>

drawer_listview.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp">

<ImageView
    android:id="@+id/DrawerIcon"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:contentDescription="@null"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/DrawerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_centerVertical="true"
    android:textColor="@color/DrawerFontColor"
    android:layout_toLeftOf="@+id/DrawerIcon"
    android:layout_toStartOf="@+id/DrawerIcon" />

</RelativeLayout>

CustomListAdapter.java

代码语言:javascript
复制
import android.app.Activity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends ArrayAdapter<String>
{
private final Activity ActivityContext;
private final String[] ItemName;
private final Integer[] ImageID;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid)
{
    super(context, R.layout.drawer_listview, itemname);

    this.ActivityContext = context;
    this.ItemName = itemname;
    this.ImageID = imgid;
}

@Override
public View getView(int position, View Unused1, ViewGroup Unused2)
{
    LayoutInflater inflater = ActivityContext.getLayoutInflater();
    View view = inflater.inflate(R.layout.drawer_listview, null);

    ImageView DrawerIcon = (ImageView) view.findViewById(R.id.DrawerIcon);
    TextView DrawerText = (TextView) view.findViewById(R.id.DrawerText);

    Typeface FontFace = Typeface.createFromAsset(ActivityContext.getAssets(), "Yekan.ttf");
    DrawerText.setTypeface(FontFace);

    DrawerIcon.setImageResource(ImageID[position]);
    DrawerText.setText(ItemName[position]);

    return view;
};
}

MainActivity.java http://paste.ubuntu.com/15237990/

EN

回答 1

Stack Overflow用户

发布于 2016-02-29 08:06:54

当用户滚动列表视图时,是否希望标题从屏幕顶部滚动?如果是这样,只需使用addHeaderView接口在列表视图中添加一个头部。

如果希望标头保持不变,请将其添加到XML中

代码语言:javascript
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Header text"/>
    <ListView
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>
</LinearLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35689892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档