最新消息:文章中包含代码时,请遵守代码高亮规范!

【原创】可上下左右滑的listview

Android kyle 1666浏览 0评论

图:

SVID_20160513_160227

 

 

之前没有用到过这样需求的listview,最近项目需求,所以写了一下,很顺利,下面上代码

1、activity_test

<?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="vertical">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_one"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="一年级"
                    android:textColor="@color/black"
                    android:textSize="@dimen/medium_text_size" />

                <TextView
                    android:id="@+id/tv_two"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="二年级"
                    android:textColor="@color/black"
                    android:textSize="@dimen/medium_text_size" />

                <TextView
                    android:id="@+id/tv_three"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="三年级"
                    android:textColor="@color/black"
                    android:textSize="@dimen/medium_text_size" />


                <TextView
                    android:id="@+id/tv_four"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="四年级"
                    android:textColor="@color/black"
                    android:textSize="@dimen/medium_text_size" />

                <TextView
                    android:id="@+id/tv_five"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="五年级"
                    android:textColor="@color/black"
                    android:textSize="@dimen/medium_text_size" />

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/view"></View>

            <ListView
                android:id="@+id/lv_test"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></ListView>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

2、item_test

<?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="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_one"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="一年级"
                android:textColor="@color/black"
                android:textSize="@dimen/medium_text_size" />

            <TextView
                android:id="@+id/tv_two"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="二年级"
                android:textColor="@color/black"
                android:textSize="@dimen/medium_text_size" />

            <TextView
                android:id="@+id/tv_three"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="三年级"
                android:textColor="@color/black"
                android:textSize="@dimen/medium_text_size" />


            <TextView
                android:id="@+id/tv_four"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="四年级"
                android:textColor="@color/black"
                android:textSize="@dimen/medium_text_size" />

            <TextView
                android:id="@+id/tv_five"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="五年级"
                android:textColor="@color/black"
                android:textSize="@dimen/medium_text_size" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/view"></View>
    </LinearLayout>
</LinearLayout>

3、TestActivity(包括了Adapter)


package cn.zmit.frame.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.zmit.frame.R;

/**
* Created by kyle on 2016/5/13 0013.
*/
public class TestActivity extends Activity {
private List<String[]> lists = new ArrayList<>();
ListView mLvTest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tests);
mLvTest= (ListView) findViewById(R.id.lv_test);
for(int i=0;i<15;i++){
String[]strings={"蛮王"+i,"剑圣"+i,"赵信"+i,"小龙"+i,"大龙"+i};
lists.add(strings);
}
mLvTest.setAdapter(new MyAdapter());
}
private class MyAdapter extends BaseAdapter {

@Override
public int getCount() {
return lists.size();
}

@Override
public Object getItem(int position) {
return lists.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyHolder holder;
if (convertView == null) {
convertView = View.inflate(TestActivity.this, R.layout.item_test, null);
holder = new MyHolder();
holder.mTvOne = (TextView) convertView.findViewById(R.id.tv_one);
holder.mTvTwo = (TextView) convertView.findViewById(R.id.tv_two);
holder.mTvThree = (TextView) convertView.findViewById(R.id.tv_three);
holder.mTvFour = (TextView) convertView.findViewById(R.id.tv_four);
holder.mTvFive = (TextView) convertView.findViewById(R.id.tv_five);
convertView.setTag(holder);
}else {
holder= (MyHolder) convertView.getTag();
}
holder.mTvOne.setText(lists.get(position)[0]);
holder.mTvTwo.setText(lists.get(position)[1]);
holder.mTvThree.setText(lists.get(position)[2]);
holder.mTvFour.setText(lists.get(position)[3]);
holder.mTvFive.setText(lists.get(position)[4]);
return convertView;
}
}

private class MyHolder {
private TextView mTvOne;
private TextView mTvTwo;
private TextView mTvThree;
private TextView mTvFour;
private TextView mTvFive;
}
}

 

 

 

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/16342.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者kyle的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!