侧滑,头条,XlistView

有XListView效果

//权限

<uses-permission android:name="android.permission.INTERNET"/>

//ImagetLolder            name=“”


<android.support.v4.widget.DrawerLayout 
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/fl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button3" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button2" />

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <ImageView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher" />
    </LinearLayout>


</android.support.v4.widget.DrawerLayout>

fragment1.xml

fragment2.xml

//无

fragment3.xml

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

    <HorizontalScrollView
        android:id="@+id/hsv"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <LinearLayout
            android:id="@+id/hll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"></LinearLayout>


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

</LinearLayout>


//导XListView


fragment_fxf.xml       //XListView

  <com.example.fxf.lianxi01.View.XListView
        android:id="@+id/xlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></com.example.fxf.lianxi01.View.XListView>


2种布局文件   item

lv_item1_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/img"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题加载中..."
            android:gravity="center"
            android:textSize="20dp"
            />
        <TextView
            android:id="@+id/summary"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="内容加载中..."
            android:textSize="20dp"
            />

    </LinearLayout>
</LinearLayout>

lv_item2_layout.xml

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="500dp"
        android:layout_height="200dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题加载中..."
            android:gravity="center"
            android:textSize="20dp"
            />
        <TextView
            android:id="@+id/summary"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="内容加载中..."
            android:textSize="20dp"
            />
    </LinearLayout>
    <ImageView
        android:id="@+id/img"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher"
        />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button3 = (Button) findViewById(R.id.button3);
        final Button button2 = (Button) findViewById(R.id.button2);
        final Button button1 = (Button) findViewById(R.id.button);
        button3.setTextColor(Color.RED);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        getSupportFragmentManager().beginTransaction().add(R.id.fl, fragment1).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fl, fragment2).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fl, fragment3).commit();
        getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
        getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
        getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
        getSupportFragmentManager().beginTransaction().show(fragment1).commit();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button1.setTextColor(Color.RED);
                button2.setTextColor(Color.BLACK);
                button3.setTextColor(Color.BLACK);
                getSupportFragmentManager().beginTransaction().show(fragment1).commit();
                getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
                getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button1.setTextColor(Color.BLACK);
                button2.setTextColor(Color.RED);
                button3.setTextColor(Color.BLACK);
                getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
                getSupportFragmentManager().beginTransaction().show(fragment2).commit();
                getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button1.setTextColor(Color.BLACK);
                button2.setTextColor(Color.BLACK);
                button3.setTextColor(Color.RED);
                getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
                getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
                getSupportFragmentManager().beginTransaction().show(fragment3).commit();
            }
        });
    }
}




Fragment1.java               Fragment2.java

public class Fragment1 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment1, container, false);
        return inflate;

    }
}
Fragment3.java

public class Fragment3 extends Fragment {
    final String[] titles = new String[]{"新闻", "快讯", "头条", "广告", "美股", "港股", "基金", "理财"};
    private LinearLayout hll;
    private ViewPager hvp;
    private List<TextView> textlist = new ArrayList<>();
    private View view;
    private HorizontalScrollView hsv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment3, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        hsv = (HorizontalScrollView) view.findViewById(R.id.hsv);
        hll = (LinearLayout) view.findViewById(R.id.hll);
        hvp = (ViewPager) view.findViewById(R.id.hvp);
        for (int i = 0; i < titles.length; i++) {
            TextView textView = new TextView(getActivity());
            textView.setText(titles[i]);
            textView.setTextSize(18);
            textView.setWidth(150);
            textView.setId(i + 1000);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int id = view.getId();
                    hvp.setCurrentItem(id - 1000);
                }
            });
            if (i == 0) {
                textView.setTextColor(Color.RED);
            } else {
                textView.setTextColor(Color.BLACK);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(20, 10, 20, 10);
            //添加到布局
            hll.addView(textView, layoutParams);
            //添加到集合
            textlist.add(textView);
        }
        hvp.setAdapter(new MAdapter(getActivity().getSupportFragmentManager()));
        hvp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (int i = 0; i < textlist.size(); i++) {
                    if (position == i) {
                        textlist.get(i).setTextColor(Color.RED);
                    } else {
                        textlist.get(i).setTextColor(Color.BLACK);
                    }
                }
                TextView textView = textlist.get(position);
                int i = textView.getWidth() + 10;
                hsv.scrollTo(i * position, 0);
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class MAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            //http://mnews.gw.com.cn/wap/data/news/分类标识/page_页数.json
            String[] title = new String[]{"xbsjxw", "txs", "toutiao", "news/mobile/jbgg", "news/mobile/mgxw",
                    "news/mobile/ggxw", "news/mobile/jjxw", "news/mobile/lcxw"};
            return Fragment_fxf.getinstans("http://mnews.gw.com.cn/wap/data/news/" + title[position] + "/page_");
        }

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

LvBean.java

public class LvBean {
    private String title;
    private String summary;
    private String img;

    public LvBean(String title, String summary, String img) {
        this.title = title;
        this.summary = summary;
        this.img = img;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

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

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    @Override
    public String toString() {
        return "LvBean{" +
                "title='" + title + '\'' +
                ", summary='" + summary + '\'' +
                ", img='" + img + '\'' +
                '}';
    }
}




Fragment_fxf.java

public class Fragment_fxf extends Fragment implements XListView.IXListViewListener {
    List<LvBean> list = new ArrayList<>();
    String title;
    int n = 1;
    DisplayImageOptions options;
    XListView xlv;
    private View view;
    private MAdapter adapter;

    public static Fragment_fxf getinstans(String title) {
        Fragment_fxf fragment_fxf = new Fragment_fxf();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment_fxf.setArguments(bundle);
        return fragment_fxf;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_fxf, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //sd卡
        options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)   //sd卡
                .cacheInMemory(true)
                .build();
        //接收路径
        Bundle arguments = getArguments();
        title = arguments.getString("title");
        //初始化控件
        xlv = (XListView) view.findViewById(R.id.xlv);
        //必须写
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
        Log.e("Conan", title + "1.json");
        adapter = new MAdapter();
        xlv.setAdapter(adapter);
        new MAsyncTask().execute(title + "1.json");

    }

    public void onLoad() {
        xlv.stopRefresh();
        xlv.stopLoadMore();
        xlv.setRefreshTime("刚刚");
    }

    //下拉刷新
    @Override
    public void onRefresh() {
        list.clear();
        new MAsyncTask().execute(title + "1.json");
        onLoad();
    }

    @Override
    public void onLoadMore() {
        n++;
        new MAsyncTask().execute(title + n + ".json");
        onLoad();
    }

    //网络获取
    class MAsyncTask extends AsyncTask<String, Void, String> {
        private StringBuilder builder;

        @Override
        protected String doInBackground(String... strings) {
            try {
                //获取url
                URL url = new URL(strings[0]);
                //请求网络
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int code = urlConnection.getResponseCode();
                //判断是否返回成功
                if (code == 200) {
                    //获取网络信息
                    InputStream inputStream = urlConnection.getInputStream();
                    BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
                    builder = new StringBuilder();
                    String s = "";
                    //拼接
                    while ((s = bf.readLine()) != null) {
                        builder.append(s);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return builder.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.e("Conan", s + "+-+-+-+-+-+-+-+-ss");
            try {
                JSONArray array = new JSONArray(s);
                JSONObject object = array.optJSONObject(0);
                JSONArray array2 = object.optJSONArray("data");
                for (int i = 0; i < array2.length(); i++) {
                    String titleJson = array2.optJSONObject(i).optString("title");
                    String summaryJson = array2.optJSONObject(i).optString("summary");
                    String imgJson = array2.optJSONObject(i).optString("img");
                    if (imgJson.equals("")) {
                        list.add(new LvBean(titleJson, summaryJson, "http://news.xinhuanet.com/politics/2017-09/30/1121752088_15067520678551n.jpg"));
                    } else {
                        list.add(new LvBean(titleJson, summaryJson, imgJson));
                    }
                }

                adapter.notifyDataSetChanged();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    //适配器
    class MAdapter extends BaseAdapter {

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

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

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            int type = getItemViewType(i);
            switch (type) {
                case 0:
                    ViewHodler vh;
                    if (view == null) {
                        view = View.inflate(getActivity(), R.layout.lv_item1_layout, null);
                        vh = new ViewHodler();
                        vh.title = (TextView) view.findViewById(R.id.title);
                        vh.summary = (TextView) view.findViewById(R.id.summary);
                        vh.img = (ImageView) view.findViewById(R.id.img);
                        view.setTag(vh);
                    } else {
                        vh = (ViewHodler) view.getTag();
                    }

                    vh.title.setText(list.get(i).getTitle());
                    vh.summary.setText(list.get(i).getSummary());
                    ImageLoader.getInstance().displayImage(list.get(i).getImg(), vh.img, options);
                    break;
                case 1:
                    ViewHodler vh1;
                    if (view == null) {
                        view = View.inflate(getActivity(), R.layout.lv_item2_layout, null);
                        vh1 = new ViewHodler();
                        vh1.title = (TextView) view.findViewById(R.id.title);
                        vh1.summary = (TextView) view.findViewById(R.id.summary);
                        vh1.img = (ImageView) view.findViewById(R.id.img);
                        view.setTag(vh1);
                    } else {
                        vh1 = (ViewHodler) view.getTag();
                    }

                    vh1.title.setText(list.get(i).getTitle());
                    vh1.summary.setText(list.get(i).getSummary());
                    ImageLoader.getInstance().displayImage(list.get(i).getImg(), vh1.img, options);
                    break;

            }
            return view;
        }

        @Override
        public int getItemViewType(int position) {
            if (position % 2 == 0) {
                return 0;
            } else {
                return 1;
            }
        }

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

        class ViewHodler {
            TextView title;
            TextView summary;
            ImageView img;
        }
    }
}

ImagerLolder包

imagerLolder

public class MApp extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        File file = getExternalCacheDir();
        ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).memoryCacheExtraOptions(200, 200)//配置内存缓存图片的尺寸
                .memoryCacheSize(2 * 1024 * 1024)//配置内存缓存的大小
                .threadPoolSize(3)//配置加载图片的线程数
                .threadPriority(1000)//配置线程的优先级
                .diskCache(new UnlimitedDiskCache(file))//UnlimitedDiskCache 限制这个图片的缓存路径
                .diskCacheFileCount(50)//配置sdcard缓存文件的数量
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//MD5这种方式生成缓存文件的名字
                .diskCacheSize(50 * 1024 * 1024)//在sdcard缓存50MB  .memoryCacheExtraOptions(200, 200)//配置内存缓存图片的尺寸
                .memoryCacheSize(2 * 1024 * 1024)//配置内存缓存的大小
                .threadPoolSize(3)//配置加载图片的线程数
                .threadPriority(1000)//配置线程的优先级
                .diskCache(new UnlimitedDiskCache(file))//UnlimitedDiskCache 限制这个图片的缓存路径
                .diskCacheFileCount(50)//配置sdcard缓存文件的数量
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//MD5这种方式生成缓存文件的名字
                .diskCacheSize(50 * 1024 * 1024)//在sdcard缓存50MB
        .build();
        ImageLoader.getInstance().init(build);
    }
}





1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值