在列表项中显示箭头

时间:2014-06-21 18:16:28

标签: android listview

我在片段中有一个ListView,我想在每个列表项的右侧显示一个箭头。我遵循了本教程:http://willvk.blogspot.it/2011/07/adding-arrow-image-to-right-of-listview.html但是我打开片段时唯一看到的是一个空白页面。这是我的代码:

public class MyFragment extends ListFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        String[] values = new String[] { "Item1", "Item2", "Item3",
        "Item4"};

        boolean[] listImages={true, true, true, true, true};
        setListAdapter(new CustomAdapter(getActivity(), R.layout.fragment_my, R.id.text1, R.id.image1, values, listImages ));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

    @Override
    public void onListItemClick(ListView list, View v, int position, long id) {
        Intent intent = null;
        switch(position) {
        case 0:
            intent = new Intent(getActivity(), Item1Activity.class);
            break;
        case 1:
            intent = new Intent(getActivity(), Item2Activity.class);
            break;
        case 2:
            intent = new Intent(getActivity(), Item3Activity.class);
            break;
        case 3:
            intent = new Intent(getActivity(), Item4Activity.class);
            break;

        }
        startActivity(intent);
    }
}

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<String>
{
    Activity context;
    String[] items;
    boolean[] arrows;
    int layoutId;
    int textId;
    int imageId;

    public CustomAdapter(Activity context, int layoutId, int textId, int imageId, String[] items, boolean[] arrows)
    {
        super(context, layoutId, items);

        this.context = context;
        this.items = items;
        this.arrows = arrows;
        this.layoutId = layoutId;
        this.textId = textId;
        this.imageId = imageId;
    }

    public View getView(int pos, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater=context.getLayoutInflater();
        View row=inflater.inflate(layoutId, null);
        TextView label=(TextView)row.findViewById(textId);

        label.setText(items[pos]);

        if (arrows[pos])
        {
         ImageView icon=(ImageView)row.findViewById(imageId); 
            icon.setImageResource(R.drawable.arrow);
        }   

        return(row);
    }

fragment_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:textSize="16sp"
        android:typeface="sans" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

从布局文件中删除ListView,无需覆盖ListFragment的onCreateView方法, 从您的班级

中删除以下方法
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

答案 1 :(得分:0)

您需要将您的片段附加到活动片段上,而这些片段并不意味着自己运行 看看这个教程 https://www.youtube.com/watch?v=ROfJkJqqUYY

答案 2 :(得分:0)

请在下面找到右侧有箭头的列表项目的答案。

<强> ListItems.java

import android.app.ListActivity; import android.os.Bundle;

公共类ListItems扩展了ListActivity {

String[] listItems={"alpha", "beta", "gamma", "delta", "epsilon"};
 boolean[] listImages={true, true, true, false, true};

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_list);

        setListAdapter(new ImageAdapter(this, R.layout.main_list, R.id.text1, R.id.image1, listItems, listImages ));
    }

}

<强> ImageAdapter.Java

import android.app.Activity;
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 ImageAdapter extends ArrayAdapter
{
    Activity context;
    String[] items;
    boolean[] arrows;
    int layoutId;
    int textId;
    int imageId;

    ImageAdapter(Activity context, int layoutId, int textId, int imageId, String[] items, boolean[] arrows)
    {
        super(context, layoutId, items);

        this.context = context;
        this.items = items;
        this.arrows = arrows;
        this.layoutId = layoutId;
        this.textId = textId;
        this.imageId = imageId;
    }

    public View getView(int pos, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater=context.getLayoutInflater();
        View row=inflater.inflate(layoutId, null);
        TextView label=(TextView)row.findViewById(textId);

        label.setText(items[pos]);

        if (arrows[pos])
        {
         ImageView icon=(ImageView)row.findViewById(imageId); 
            icon.setImageResource(R.drawable.ic_btn_search_go);
        }   

        return(row);
    }
}

main_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layercontainer"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#ffffff">
   <ListView
   android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
   <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000000"   
    android:typeface="sans"/>
   <ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"/>
</RelativeLayout>