当前位置: 代码迷 >> 综合 >> 侧滑,头条,XlistView
  详细解决方案

侧滑,头条,XlistView

热度:25   发布时间:2023-11-15 20:03:26.0

有XListView效果

//权限

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

//ImagetLolder            name=“”


<android.support.v4.widget.DrawerLayout ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:id="@+id/fl"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"></FrameLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Button3" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Button2" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Button" /></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="left"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView" /><ImageViewandroid: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"><HorizontalScrollViewandroid:id="@+id/hsv"android:layout_width="match_parent"android:layout_height="50dp"><LinearLayoutandroid:id="@+id/hll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"></LinearLayout></HorizontalScrollView><android.support.v4.view.ViewPagerandroid: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.XListViewandroid: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"><ImageViewandroid:id="@+id/img"android:layout_width="200dp"android:layout_height="200dp"android:src="@mipmap/ic_launcher"/><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="200dp"><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="标题加载中..."android:gravity="center"android:textSize="20dp"/><TextViewandroid: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"><LinearLayoutandroid:orientation="vertical"android:layout_width="500dp"android:layout_height="200dp"><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="标题加载中..."android:gravity="center"android:textSize="20dp"/><TextViewandroid:id="@+id/summary"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="内容加载中..."android:textSize="20dp"/></LinearLayout><ImageViewandroid: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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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@Overridepublic 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@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment3, container, false);return view;}@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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);}@Overridepublic void onPageSelected(int position) {}@Overridepublic void onPageScrollStateChanged(int state) {}});}class MAdapter extends FragmentPagerAdapter {public MAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {//http://mnews.gw.com.cn/wap/data/news/分类标识/page_页数.jsonString[] 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_");}@Overridepublic 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;}@Overridepublic 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@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_fxf, container, false);return view;}@Overridepublic 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("刚刚");}//下拉刷新@Overridepublic void onRefresh() {list.clear();new MAsyncTask().execute(title + "1.json");onLoad();}@Overridepublic void onLoadMore() {n++;new MAsyncTask().execute(title + n + ".json");onLoad();}//网络获取class MAsyncTask extends AsyncTask<String, Void, String> {private StringBuilder builder;@Overrideprotected String doInBackground(String... strings) {try {//获取urlURL 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();}@Overrideprotected 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 {@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int i) {return list.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic 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;}@Overridepublic int getItemViewType(int position) {if (position % 2 == 0) {return 0;} else {return 1;}}@Overridepublic int getViewTypeCount() {return 2;}class ViewHodler {TextView title;TextView summary;ImageView img;}}
}

ImagerLolder包

imagerLolder

public class MApp extends Application{@Overridepublic 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);}
}