如何向listview添加标签?

时间:2017-08-17 22:18:42

标签: android listview

我需要在我的列表中添加两个标签,我不知道该怎么做,我想在第一行中添加一个,在第四个分隔后添加另一个:趋势和流派,我看到了一些关于添加的内容新的xml布局,但我需要帮助,因为我不能,这只是项目之间的文本..

我的主要活动:

public class inicio extends AppCompatActivity {

    private ListView listaitens;
    private String[] itens = {
            "Home", "playlist", "trend", "Conta", "Live", "rock", "Hip Hop", "Pop",
            "House", "Gospel Music", "Metal", "Alt Rock", "Sport", "Game"
    };
    Integer[] imgid={
            R.drawable.home,
            R.drawable.playlist,
            R.drawable.trend,
            R.drawable.conta,
            R.drawable.live,
            R.drawable.rock,
            R.drawable.hiphop,
            R.drawable.pop,
            R.drawable.house,
            R.drawable.gospel,
            R.drawable.metal,
            R.drawable.alternative_rock,
            R.drawable.sport,
            R.drawable.game,
    };

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

        listaitens = (ListView) findViewById(R.id.listview);

        CustomListAdapter adapter=new CustomListAdapter(this, itens, imgid);
        listaitens=(ListView)findViewById(R.id.listview);
        listaitens.setAdapter(adapter);

        listaitens.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                if (position == 0) {
                    Intent intent = new Intent(inicio.this, INSMainActivity.class);
                    intent.putExtra("link", "https://m.youtube.com/");
                    startActivity(intent);

我的适配器:

public class CustomListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] itens;
    private final Integer[] imgid;

    public CustomListAdapter(inicio context, String[] itens, Integer[] imgid) {
        super(context, R.layout.mylist, itens);

        this.context=context;
        this.itens=itens;
        this.imgid=imgid;
    }

    public View getView(int position,View view,ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.mylist, null,true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

        txtTitle.setText(itens[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Access "+itens[position]);
        return rowView;

    }

xml listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#000000" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

如果有人可以帮助我,非常感谢你。

1 个答案:

答案 0 :(得分:0)

您必须创建单独的xml文件:

  • 一个用于创建行布局
  • 另一个标签

然后按以下方式更改适配器

public class CustomListAdapter extends ArrayAdapter<String> {

private static final int TYPE_ITEM = 0;
private static final int TYPE_TAB = 1;

private final Activity context;
private final String[] itens;
private final Integer[] imgid;

public CustomListAdapter(inicio context, String[] itens, Integer[] imgid) {
    super(context, R.layout.mylist, itens);

    this.context=context;
    this.itens=itens;
    this.imgid=imgid;
}

public View getView(int position,View view,ViewGroup parent) {
    int type = getItemViewType(position);

    LayoutInflater inflater=context.getLayoutInflater();

    if (view == null) {
          // Inflate the layout according to the view type
          LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (type == TYPE_ITEM) {
         view = inflater.inflate(R.layout.your_item_layout, parent, false);
    } else {
         view = inflater.inflate(R.layout.your_tab_layout, parent, false);
    }

   TextView txtTitle = (TextView) view.findViewById(R.id.item);
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    TextView extratxt = (TextView) view.findViewById(R.id.textView1);

    /**
    * Your tab layout textView
    */
    if (type == TYPE_TAB) {
      TextView tab = (TextView) view.findViewById(R.id.tab);
      tab.setText("Your text");
    }

    txtTitle.setText(itens[position]);
    imageView.setImageResource(imgid[position]);
    extratxt.setText("Access "+itens[position]);

    return view;

}

/**
* Override this function, returns the number of types of views in your list. 
* In your case it is 2
*/
@Override
    public int getViewTypeCount() {
        return 2;
    }

/**
* Override this function, returns the item to be return at position
*/
@Override
    public int getItemViewType(int position) {
        //return type = tab according to your position
        if (position == 0 || position == 4) { 
          return TYPE_TAB;
        } else {
          return TYPE_ITEM;
        }
    }