Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——ListView布局+適配器(三)

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

編輯:關於Android編程

\

\

 

 

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 list;
    private int pageCount=1;

    private ViewPager vp;
    private List 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);
    }
}









    
    
    
        
        
        
    

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 imgList;

    public MyViewPagerAdapter(List 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 list;

    public MyListViewAdapter(Context context, List 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 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() {
            //下拉
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                pageCount=1;
                list.clear();
                new MyRefresh().execute();
            }
            //上拉
            @Override
            public void onPullUpToRefresh(PullToRefreshBase 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{
        @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();

        }
    }


}







 

 

 

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 list;

    public MyListViewAdapter(Context context, List 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;
    }

}



    
    



\

 

\

\

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved