Fragment加xlistView

activity_main.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/txt1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="25sp"
        android:layout_height="wrap_content"
        android:text="页面一"/>
    <TextView
        android:id="@+id/txt2"
        android:layout_width="0dp"
        android:textSize="25sp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="页面二"/>
</LinearLayout>
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/vp"></android.support.v4.view.ViewPager>
## MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private TextView txt1;
private TextView txt2;
private ViewPager vp;
private List<Fragment> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //找控件
    txt1 = findViewById(R.id.txt1);
    txt2 = findViewById(R.id.txt2);
    vp = findViewById(R.id.vp);
    //创建list容器
    list = new ArrayList<>();
    //添加到list集合
    list.add(new AFragment());
    list.add(new BFragment());

    txt1.setOnClickListener(this);
    txt2.setOnClickListener(this);

    vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

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

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

        }

        @Override
        public void onPageSelected(int i) {

        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });


}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.txt1:
            vp.setCurrentItem(0);
            txt1.setBackgroundColor(Color.GREEN);
            txt2.setBackgroundColor(Color.WHITE);
            break;
         case R.id.txt2:
            vp.setCurrentItem(1);
            txt1.setBackgroundColor(Color.WHITE);
            txt2.setBackgroundColor(Color.GREEN);
            break;
    }
}

}

afragment.xml

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

<com.bwie.xlistview.XListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/xlist"></com.bwie.xlistview.XListView>
## AFragment

public class AFragment extends Fragment {

private XListView xlist;
private List<News.DataBean> list;
private MyAdapter adapter;
public static final String url = "http://www.xieast.com/api/news/news.php?type=top&page=";
public int page = 1;
Handler handler = new Handler();

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = View.inflate(getActivity(),R.layout.afragment,null);
    xlist = view.findViewById(R.id.xlist);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    list = new ArrayList<>();
    //创建适配器
    adapter = new MyAdapter(getActivity(), list);
    xlist.setAdapter(adapter);

    xlist.setPullLoadEnable(true);
    xlist.setPullRefreshEnable(true);

    getDate(1,false);

}

private void getDate(int i, final boolean b) {
    new AsyncTask<String, Integer, String>() {
        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.getFromFile(strings[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            News news = gson.fromJson(s,News.class);
            if (news != null){
                if (!b){
                    list.clear();
                }
                list.addAll(news.getData());
                adapter.notifyDataSetChanged();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (b){
                            //停止加载更多
                            xlist.stopLoadMore();
                        }else {
                            xlist.stopRefresh();
                            xlist.setRefreshTime("刚刚");
                        }
                    }
                },3000);
            }
        }
    }.execute(url+page);
}

}

MyAdapter

public class MyAdapter extends BaseAdapter {

private Context context;
private List<News.DataBean> list;

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

@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) {
    ViewHolder holder = null;
    if (convertView == null){
        convertView = View.inflate(context,R.layout.item,null);
        holder = new ViewHolder();
        holder.image = convertView.findViewById(R.id.image);
        holder.title = convertView.findViewById(R.id.title);
        holder.date = convertView.findViewById(R.id.date);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.image);
    holder.title.setText(list.get(position).getTitle());
    holder.date.setText(list.get(position).getDate());
    return convertView;
}
class ViewHolder{
    ImageView image;
    TextView title;
    TextView date;
}

}

item.xml

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

<ImageView
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:id="@+id/image"
    android:src="@drawable/ic_launcher_background"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title"
        android:textSize="22sp"
        android:id="@+id/title"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title"
        android:textSize="18sp"
        android:id="@+id/date"/>
</LinearLayout>
## MyApplication

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(build);
}
}

HttpUtils

public class HttpUtils {
public static String getFromFile(String string) {
String result = “”;
try {
URL url = new URL(string);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setUseCaches(false);
connection.setConnectTimeout(3000);
connection.setRequestMethod(“GET”);
if (connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
result = getFromFileHttp(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

private static String getFromFileHttp(InputStream is) {
    String result = "";
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer,0,buffer.length)) != -1){
            baos.write(buffer,0,len);
            baos.flush();
        }
        result = baos.toString();
        baos.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值