Fragment+多条目加载+下拉刷新

//主布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.a1.rikao10.activity.MainActivity"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    >

  <android.support.v4.view.ViewPager
      android:id="@+id/v_pager"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      ></android.support.v4.view.ViewPager>

<RelativeLayout
    android:layout_gravity="start"
    android:layout_width="150dp"
    android:layout_height="match_parent"
    android:background="#FFD0E5"
    >
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/t_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageView"
        android:layout_marginTop="25dp"
        android:text="页面一"
        android:textSize="25sp"
        />

    <TextView
        android:id="@+id/t_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/t_one"
        android:layout_marginTop="21dp"
        android:text="页面二"
        android:textSize="25sp"
        />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

//Send_layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="页面一"
        android:textSize="30sp"
        />

    <View
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#000"
        />
    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:id="@+id/pull_to_refresh_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <com.example.a1.rikao10.listviews.MyListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></com.example.a1.rikao10.listviews.MyListView>
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>

</RelativeLayout>

//子布局

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/t_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/t_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/t_image"
        android:layout_toRightOf="@+id/t_image"
        android:text="Text"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/t_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/t_image"
        android:layout_marginBottom="12dp"
        android:layout_toEndOf="@+id/t_image"
        android:layout_toRightOf="@+id/t_image"
        android:text="TextView"
        android:textSize="20sp"
        />
</RelativeLayout>

//send子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/two_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="19dp"
        android:textSize="25sp"
        />
</LinearLayout>

//ListAdapter适配器

public class ListAdapter extends BaseAdapter{
    private static final String TAG = "ListAdapter";
    private Context context;
    private List<NewsBean.DataBean>list;

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

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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


    private final int ITEN_VIEW_TYPE_ONE = 1;   // item1 的类型
    private final int ITEM_VIEW_TYEP_TWO = 2;   // item2 的类型

    @Override
    public int getItemViewType(int position) {
        // 根据position 来判断 给出 item类型

        if (position % 2 == 0) {
            // 偶数
            return ITEN_VIEW_TYPE_ONE;
        } else{
            // 奇数
            return  ITEM_VIEW_TYEP_TWO;
        }
        /*int type = list.get(position).getImpressType();
        switch (type) {
            case 0:
                return ITEN_VIEW_TYPE_ONE;
            case 1:
                return ITEM_VIEW_TYEP_TWO;
        }
        return
*/
        // String type=list.get(position).getType();
       /* if (type.equals("41")){
            return  0;
        }*/
        /*if ("41".equals(type)) {
            return 0;
        }
        return 1;*/
    }

    @Override
    public int getViewTypeCount() {

        return 2+1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type=getItemViewType(position);
        Log.d(TAG, "123123getView: "+type);
        switch (type){
            case ITEN_VIEW_TYPE_ONE:
                ViewHolder holder;
                if (convertView==null){
                 convertView=View.inflate(context, R.layout.item_layout,null);
                 holder=new ViewHolder();
                 holder.imageView=convertView.findViewById(R.id.t_image);
                 holder.textView01=convertView.findViewById(R.id.t_title);
                 holder.textView02=convertView.findViewById(R.id.t_time);
                 convertView.setTag(holder);
                }else{
                    holder= (ViewHolder) convertView.getTag();
                }
                    holder.textView01.setText(list.get(position).getTitle());
                    holder.textView02.setText(String.valueOf(list.get(position).getTopTime()));
                   try {
                       String userImg= list.get(position).getImg();
                       Log.d(TAG, "123333311111111111111111111111getView: "+userImg);
                       ImageLoader.getInstance().displayImage(userImg, holder.imageView);
                   }catch (Exception e){
                       Log.d(TAG, "getView: "+e.getMessage());
                   }

              break;
            case ITEM_VIEW_TYEP_TWO:
                ViewHolder2 holder2;
                if (convertView==null){
                    convertView=View.inflate(context,R.layout.two_item_layout,null);
                    holder2=new ViewHolder2();
                   holder2.textView03=convertView.findViewById(R.id.two_title);
                   convertView.setTag(holder2);
                }else {
                    holder2= (ViewHolder2) convertView.getTag();
                }
//                try {
                    holder2.textView03.setText(list.get(position).getTitle());
//                } catch (Exception e) {

//                }
                break;
        }
        return convertView;
    }
    class ViewHolder{
        ImageView imageView;
        TextView textView01;
        TextView textView02;

    }
    class  ViewHolder2{
            TextView textView03;
    }
}

//Fragment适配器

public class FragmentAdapter extends FragmentPagerAdapter{
    private List<Fragment>list;

    public FragmentAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list= list;
    }

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

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

//MyListView

public class MyListView extends ListView{

    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heigtSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heigtSpec);
    }
}

//主Acivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 private TextView one,two;
private Fragment01 fragment01;
private Fragment02 fragment02;
private ViewPager viewPager;
private List<Fragment>fs=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        one=findViewById(R.id.t_one);
        two=findViewById(R.id.t_two);
        viewPager=findViewById(R.id.v_pager);
        fragment01=new Fragment01();
        fragment02=new Fragment02();

        fs.add(fragment01);
        fs.add(fragment02);

        FragmentAdapter fragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),fs);
        viewPager.setAdapter(fragmentAdapter);
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,SencodActivty.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View view) {
      switch (view.getId()){
          case R.id.two_iamge:

              break;
      }
    }
}

//SendActiivty

public class SencodActivty extends AppCompatActivity{
    private static final String TAG = "SencodActivty=====";
    public static String one_url="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
    private MyListView listView;
    private  List<NewsBean.DataBean>list=new ArrayList<>();
    private PullToRefreshScrollView pullToRefreshScrollView;
    private int page=1;
    private ListAdapter listAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_layout);
        initViews();
        listAdapter=new ListAdapter(SencodActivty.this,list);
        listView.setAdapter(listAdapter);
        initDatas();
    }

    private void initDatas() {
            HttpUtils httpUtils=HttpUtils.getInstance();
            httpUtils.get(one_url);
            httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() {
                @Override
                public void getSuccess(String json) {
                    Log.d(TAG, "成功"+json);
                    Gson gson=new Gson();
                    NewsBean newsBean=gson.fromJson(json,NewsBean.class);
                    List<NewsBean.DataBean> shuju_list=newsBean.getData();

                    if (page==1){
                        list.clear();
                    }
                    list.addAll(shuju_list);
                    listAdapter.notifyDataSetChanged();
                    pullToRefreshScrollView.onRefreshComplete();
                }

                @Override
                public void getError(String error) {
                    Log.d(TAG,"失败"+error);
                }
            });

    }

    private void initViews() {
        listView=findViewById(R.id.list_view);
        pullToRefreshScrollView=findViewById(R.id.pull_to_refresh_scrollview);
        pullToRefreshScrollView.setMode(PullToRefreshScrollView.Mode.BOTH);
       pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
           @Override
           public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
               page=1;
               initDatas();
           }

           @Override
           public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
            page++;
            initDatas();
           }
       });
    }
}



猜你喜欢

转载自blog.csdn.net/air_show/article/details/80707243
今日推荐