Viewpager+Fragment嵌套Tablelayout+Xlistview下拉刷新+Imageloader

清单文件配置



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lxzk">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:name=".App"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局文件

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:orientation="horizontal"

    >
    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000"
        />

    <TextView
        android:id="@+id/t2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        />




</LinearLayout>

<RadioGroup
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    >
    <RadioButton
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="首页"
        android:gravity="center"

        android:button="@null"
        android:layout_weight="1"
        />
    <RadioButton
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="电影"
        android:button="@null"
        android:layout_weight="1"
        />




</RadioGroup>
<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/ic_launcher_background"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="大卫"
    android:id="@+id/t1"
    android:textSize="20dp"
    android:layout_marginLeft="120dp"
    android:layout_marginTop="5dp"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="大卫"
    android:id="@+id/t2"
    android:textSize="17dp"
    android:layout_marginLeft="120dp"
    android:layout_marginTop="30dp"
    />
<?xml version="1.0" encoding="utf-8"?>

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <android.support.design.widget.TabLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

这个Fragment布局中写Xlistview

<?xml version="1.0" encoding="utf-8"?>

<!-- TODO: Update blank fragment layout -->
 <com.example.xlist.me.maxwin.view.XListView
     android:id="@+id/xlv"
     android:layout_width="match_parent"
     android:layout_height="match_parent"></com.example.xlist.me.maxwin.view.XListView>

package com.example.lxzk;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_a, container, false);
}

}

package com.example.lxzk;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;

public class App extends Application {
@Override
public void onCreate() {

    super.onCreate();

    ImageLoader.getInstance().init(ImageConfs.getcon(this));
}

}

package com.example.lxzk;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class BFragment extends Fragment {

private TabLayout table;
private ViewPager viewpager;
private ArrayList<Fragment> list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_b,container,false);

    table = view.findViewById(R.id.table);
    viewpager = view.findViewById(R.id.viewpager);

    init();
    return view;
}

private void init() {
    list = new ArrayList<>();

    list.add(new Cfragment());
    list.add(new Dfragment());

    Myadapter ma =  new Myadapter(getChildFragmentManager());
    ma.setfragments(list);
    viewpager.setAdapter(ma);

    //table.addTab(table.newTab());
    //table.addTab(table.newTab());

    table.setupWithViewPager(viewpager);

    table.getTabAt(0).setText("达瓦达瓦");
    table.getTabAt(1).setText("哦哦覅");
}

}

展示listview

package com.example.lxzk;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.Toast;

import com.example.xlist.me.maxwin.view.XListView;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

public class Cfragment extends Fragment {

private XListView xlv;
private Madapter ma;
private int page = 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = View.inflate(getActivity(), R.layout.fragment_cfragment,null);


    xlv = view.findViewById(R.id.xlv);

    ma = new Madapter(getActivity());

    xlv.setAdapter(ma);


    xlv.setPullLoadEnable(true);

    xlv.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {
            page = 1;
            LoadDate(page);
        }

        @Override
        public void onLoadMore() {
            LoadDate(page);
        }
    });
    canloadmore(true);
    LoadDate(page);
    return view;
}
private void canloadmore(boolean b) {
    xlv.setPullLoadEnable(b);
}

private void LoadDate(int page) {
    final  String path = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page="+page+"&count=3";

    String Urlwith = path+page;

    new AsyncTask<String,String,List<Person.ResultDate>>(){


        @Override
        protected List<Person.ResultDate> doInBackground(String... strings) {

            Person dian = null;

            try {
                URL url = new URL(path);

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                httpURLConnection.setReadTimeout(5000);
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.setRequestMethod("GET");

                int request = httpURLConnection.getResponseCode();
                if(request == 200){
                    String str = String2(httpURLConnection.getInputStream());
                    dian = new Gson().fromJson(str,Person.class);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return dian == null ? null:dian.getResult();

        }

        @Override
        protected void onPostExecute(List<Person.ResultDate> resultDates) {
            super.onPostExecute(resultDates);
            if(resultDates == null){
                Toast.makeText(getActivity(),"请求数据失败",Toast.LENGTH_SHORT).show();
                return;
            }
            updateData(resultDates);
            loade();
            canloadmore(resultDates.size() == 3);
        }
    }.execute(Urlwith);
}

private void loade() {
    page++;
    xlv.stopRefresh();
    xlv.stopLoadMore();
}

private void updateData(List<Person.ResultDate> resultDates) {
    if(page == 1){
        ma.setDats(resultDates);
        Date date = new Date();
        xlv.setRefreshTime(date.toLocaleString());
    }else{
        ma.addDats(resultDates);
    }
}

private String String2(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

    StringBuilder stringBuilder = new StringBuilder();

    String text = null;
    while ((text = bufferedReader.readLine()) != null){
        stringBuilder.append(text);
    }
    bufferedReader.close();
    return  stringBuilder.toString();
}

}

package com.example.lxzk;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Dfragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_dfragment, container, false);
}

}

package com.example.lxzk;

import android.content.Context;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;

public class ImageConfs {
public static ImageLoaderConfiguration getcon(Context context){
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
.memoryCacheSizePercentage(10)
.diskCacheSize(5010241024)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.writeDebugLogs()
.build();

    return configuration;
}

public static  DisplayImageOptions getpon(){
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheOnDisk(true)
            .cacheInMemory(true)
            .showImageOnLoading(R.mipmap.ic_launcher)
            .showImageOnFail(R.drawable.ic_launcher_background)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .displayer(new CircleBitmapDisplayer())
            .build();

    return options;
}

}

package com.example.lxzk;

import android.content.Context;
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 com.nostra13.universalimageloader.core.ImageLoader;

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

public class Madapter extends BaseAdapter {
List<Person.ResultDate> list;
Context context;

public Madapter(Context context) {
    this.context = context;
    list = new ArrayList<Person.ResultDate>();
}


public void  addDats(List<Person.ResultDate> dates){
    if(dates !=null){
        list.addAll(dates);
        notifyDataSetChanged();
    }
}

public  void setDats(List<Person.ResultDate> dates){
    list = dates;
    notifyDataSetChanged();
}

public void setList(List<Person.ResultDate> list){
    this.list = list;

    notifyDataSetChanged();
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolde vh;
    if(convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.cfragment_list,null);
        vh = new ViewHolde(convertView);
    }else{
        vh = (ViewHolde) convertView.getTag();
    }
    Person.ResultDate data = (Person.ResultDate) getItem(position);
    vh.BindDate(data);
    return convertView;
}
class ViewHolde{
    TextView t1;
    TextView t2;
    ImageView image;

    public ViewHolde(View view) {
        t1 = view.findViewById(R.id.t1);
        t2 = view.findViewById(R.id.t2);
        image = view.findViewById(R.id.image);
        view.setTag(this);
    }

    public void BindDate(Person.ResultDate data){
        t1.setText(data.getName());
        t2.setText(data.getSummary());
        ImageLoader.getInstance().displayImage(data.getImageUrl(),image,ImageConfs.getpon());
    }
}

}

package com.example.lxzk;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private ViewPager viewpager;
private RadioGroup group;
private TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewpager = findViewById(R.id.viewpager);
    group = findViewById(R.id.group);
    t1 = findViewById(R.id.t1);
    t2 = findViewById(R.id.t2);

    viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            switch (i){
                case 0:
                    AFragment afragment = new AFragment();
                    return afragment;
                case 1:
                    BFragment bfragment = new BFragment();
                    return bfragment;

            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }
    });


    group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.btn1:
                    viewpager.setCurrentItem(0);
                    break;
                case R.id.btn2:
                    viewpager.setCurrentItem(1);
                    break;


            }
        }
    });

    viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            switch (i){
                case 0:
                    group.check(R.id.btn1);
                    t1.setBackgroundColor(Color.BLACK);
                    t2.setBackgroundColor(Color.WHITE);

                    break;

                case 1:
                    group.check(R.id.btn2);
                    t2.setBackgroundColor(Color.BLACK);
                    t1.setBackgroundColor(Color.WHITE);

                    break;


            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
}

}

package com.example.lxzk;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

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

public class Myadapter extends FragmentPagerAdapter {

private List<Fragment> m;

public Myadapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    Fragment fragment = m.get(i);


    return fragment;
}

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

public void setfragments(ArrayList<Fragment> list) {
    m = list;
}

}

package com.example.lxzk;

import java.util.List;

public class Person {
public List result;
public String message;
public int status;

public List<ResultDate> getResult() {
    return result;
}

public void setResult(List<ResultDate> result) {
    this.result = result;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public class ResultDate{
    public String name;
    public String summary;
    public String imageUrl;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
}

}

猜你喜欢

转载自blog.csdn.net/NorthHar/article/details/83302993
今日推荐