码迷,mamicode.com
首页 > 移动开发 > 详细

Android——ListView布局+适配器(三)

时间:2016-08-05 23:10:53      阅读:869      评论:0      收藏:0      [点我收藏+]

标签:

Android——ListView布局+适配器(三)

技术分享

技术分享


package com.example.administrator.newstop;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.administrator.newstop.adapter.MyListViewAdapter;
import com.example.administrator.newstop.adapter.MyViewPagerAdapter;
import com.example.administrator.newstop.entity.News;

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

public class ListViewOnScrollActivity extends AppCompatActivity {
private ListView lv;
    private MyListViewAdapter ma;
    private  List<News> list;
    private int pageCount=1;

    private ViewPager vp;
    private List<ImageView> imgList;
    private RadioGroup rg;
    private RadioButton rb1,rb2,rb3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_on_scroll);
        lv=(ListView)findViewById(R.id.lv);
       list = new ArrayList<>();
        for(int j=0;j<=20;j++){
            list.add(new News("新闻"+j,"",R.mipmap.ic_launcher,0,0,0));
        }
        ma=new MyListViewAdapter(this,list);


        imgList=new ArrayList<>();

        ImageView img=new ImageView(this);
        img.setImageResource(R.mipmap.zyf);
        imgList.add(img);
        img=new ImageView(this);
        img.setImageResource(R.mipmap.zyfzyf);
        imgList.add(img);
        img=new ImageView(this);
        img.setImageResource(R.mipmap.zz);
        imgList.add(img);



        View v=getLayoutInflater().inflate( R.layout.view_pager,null);
        vp= (ViewPager) v.findViewById(R.id.vp);
        rb1=(RadioButton) v.findViewById(R.id.rb1);
        rb2=(RadioButton) v.findViewById(R.id.rb2);
        rb3=(RadioButton) v.findViewById(R.id.rb3);
        vp.setAdapter(new MyViewPagerAdapter(imgList));
        lv.addHeaderView(v);

        rg=(RadioGroup)v.findViewById(R.id.rg);
        ///RadioGroup监听RadioGroup
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.rb1){
                    vp.setCurrentItem(0);
                    Toast.makeText(getBaseContext(),"图片1",Toast.LENGTH_SHORT).show();
                } else if (checkedId==R.id.rb2){
                    vp.setCurrentItem(1);
                    Toast.makeText(getBaseContext(),"图片2",Toast.LENGTH_SHORT).show();
                }
                else if (checkedId==R.id.rb3){
                    vp.setCurrentItem(2);
                    Toast.makeText(getBaseContext(),"图片3",Toast.LENGTH_SHORT).show();
                }
                }

        });
        //RadioGroup监听RadioGroup
       vp .addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) rg.getChildAt(position);
                rb.setChecked(true);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



        lv.setAdapter(ma);
//        lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(getBaseContext(),""+i,Toast.LENGTH_SHORT).show();
//            }

//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//
//            }
//        });



        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                Log.e("===firstItem:",i+"");
                Log.e("===visibleItem:",i1+"");
                Log.e("===totalItem:",i2+"");
                if(i+i1==i2){
                   loadData();
                }

            }
        });
    }
    public  void  loadData(){
        for(int j=(pageCount);j<(20+pageCount);j++){
            list.add(new News("新增加新闻新闻"+j,"",R.mipmap.ic_launcher,0,0,0));
        }
        pageCount+=20;
        ma.notifyDataSetChanged();//刷新数据   lv.setAdapter(ma);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.administrator.newstop.ListViewOnScrollActivity">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv"
    ></ListView>
</RelativeLayout>

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

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/vp"
        android:background="#fbfb31">
    </android.support.v4.view.ViewPager>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/rg"
        android:layout_marginTop="-50dp"
        android:layout_gravity="center_horizontal">
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="图片1"
            android:background="#fafa03"
            android:id="@+id/rb1"
            android:button="@null"
            android:checked="true"
            android:textColor="@color/rdbt_text_color"
            android:gravity="center"
            />
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="图片2"
            android:id="@+id/rb2"
            android:button="@null"
            android:gravity="center"
            android:background="#fafa03"
            android:textColor="@color/rdbt_text_color"
            />
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="图片3"
            android:id="@+id/rb3"
            android:button="@null"
            android:gravity="center"
            android:background="#fafa03"
            android:textColor="@color/rdbt_text_color"
            />
    </RadioGroup>
</LinearLayout>

package com.example.administrator.newstop.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by 冲天之峰 on 2016/8/1.
 */
public class MyViewPagerAdapter extends PagerAdapter {
    private List<ImageView> imgList;

    public MyViewPagerAdapter(List<ImageView> imgList) {
        this.imgList = imgList;
    }
    @Override
    public int getCount() {
        return imgList.size();
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(imgList.get(position));
        return imgList.get(position);
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(imgList.get(position));
    }
}

package com.example.administrator.newstop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.newstop.Main2Activity;
import com.example.administrator.newstop.R;
import com.example.administrator.newstop.entity.News;

import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
public class MyListViewAdapter extends BaseAdapter {
    private Context context;
    private List<News> list;

    public MyListViewAdapter(Context context, List<News> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view==null){
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_simple_layout,null);
            vh.img = (ImageView) view.findViewById(R.id.iv);
            vh.title = (TextView) view.findViewById(R.id.tv);
            view.setTag(vh);
        }else {
            vh = (ViewHolder) view.getTag();
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Main2Activity.class);
                context.startActivity(intent);
            }
        });
        vh.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"点击图片",Toast.LENGTH_SHORT).show();
            }
        });
        News news = list.get(i);
        vh.title.setText(news.getTitle());
        vh.img.setImageResource(news.getImg());
        return view;
    }
    public class ViewHolder{
        TextView title;
        ImageView img;
    }

}



技术分享

技术分享

技术分享



技术分享

技术分享


package com.example.administrator.newstop;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;

import com.example.administrator.newstop.adapter.MyListViewAdapter;
import com.example.administrator.newstop.entity.News;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

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

public class PullToRefreshActivity extends AppCompatActivity {
private PullToRefreshListView pv;
    private MyListViewAdapter ma;
    private List<News> list;

    private int pageCount=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh);

        pv=(PullToRefreshListView)findViewById(R.id.pv);
        list = new ArrayList<>();
        for(int j=0;j<=20;j++){
            list.add(new News("新闻"+j,"",R.mipmap.ic_launcher,0,0,0));
        }
        ma=new MyListViewAdapter(this,list);
        pv.setAdapter(ma);

        pv.setMode(PullToRefreshBase.Mode.BOTH);
        ILoadingLayout upLoading=pv.getLoadingLayoutProxy(true,false);
        upLoading.setPullLabel("下拉客官请稍等");
        upLoading.setRefreshingLabel("客官刷新中");
        upLoading.setReleaseLabel("好了客官");
        ILoadingLayout downLoading=pv.getLoadingLayoutProxy(false,true);
        downLoading.setPullLabel("下拉客官请稍等");
        downLoading.setRefreshingLabel("客官刷新中");
        downLoading.setReleaseLabel("好了客官");
        View v=getLayoutInflater().inflate(R.layout.pv_header_layout,null);

        pv.getRefreshableView().addHeaderView(v);


        pv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageCount=1;
                list.clear();
                new MyRefresh().execute();
            }
            //上拉
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//               loadData();
//             pv.onRefreshComplete();

                new MyRefresh().execute();
            }
        });


    }
    public  void  loadData(){

        for(int j=(pageCount);j<(20+pageCount);j++){
            list.add(new News("新增加新闻新闻"+j,"",R.mipmap.ic_launcher,0,0,0));
        }
        pageCount+=20;
        ma.notifyDataSetChanged();//刷新数据   lv.setAdapter(ma);
    }

    public class  MyRefresh extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... voids) {

            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            loadData();
            pv.onRefreshComplete();

        }
    }


}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.administrator.newstop.PullToRefreshActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pv"
    app:ptrAnimationStyle="flip"
    >

</com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>


package com.example.administrator.newstop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.newstop.Main2Activity;
import com.example.administrator.newstop.R;
import com.example.administrator.newstop.entity.News;

import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
public class MyListViewAdapter extends BaseAdapter {
    private Context context;
    private List<News> list;

    public MyListViewAdapter(Context context, List<News> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view==null){
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_simple_layout,null);
            vh.img = (ImageView) view.findViewById(R.id.iv);
            vh.title = (TextView) view.findViewById(R.id.tv);
            view.setTag(vh);
        }else {
            vh = (ViewHolder) view.getTag();
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Main2Activity.class);
                context.startActivity(intent);
            }
        });
        vh.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"点击图片",Toast.LENGTH_SHORT).show();
            }
        });
        News news = list.get(i);
        vh.title.setText(news.getTitle());
        vh.img.setImageResource(news.getImg());
        return view;
    }
    public class ViewHolder{
        TextView title;
        ImageView img;
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_marginLeft="2dp"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_weight="0.6"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/iv"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_weight="0.4"
        android:id="@+id/tv"
        android:gravity="center"
        android:text="34134324124"/>


</LinearLayout>

技术分享

技术分享

技术分享




作者:冲天之峰    20160805

<pre name="code" class="java">

Android——ListView布局+适配器(三)

标签:

原文地址:http://blog.csdn.net/zhangyufeng0126/article/details/52132984

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!