一、说在前面
1、今天任务:整改新闻首页,用viewpage来区别不同类别的新闻(5h)
2、明天任务:完善个人信息界面(预计2h)
3、遇到问题:如果轮播图被向上滑动一点时就失去了轮播功能(所以没改还是固定)
二、效果
三、代码
1、ListFragment.java
package com.me.fragment; import android.content.Intent; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.ListPopupWindow; import androidx.fragment.app.Fragment; import androidx.viewpager2.widget.ViewPager2; import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayoutMediator; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.me.ViewModel.NewsViewModel; import com.me.adapter.ListPopupWindowAdapter; import com.me.adapter.TabAdapter; import com.me.domain.New_; import com.me.domain.News; import com.me.jk.ImgViewGroupLister; import com.me.news_2.NewsActivity; import com.me.news_2.R; import com.me.util.HttpUtil; import java.util.ArrayList; import java.util.List; public class ListFragment extends Fragment implements ImgViewGroupLister { private ViewPager2 viewPager2; private TabLayout tabLayout; private List<Fragment> data = new ArrayList<>(); private EditText mEditText; private ImageView imageView; private SentUrlTask sentUrlTask; private SearchTask searchTask; private ListPopupWindow mListPop ; private List<News> lists = new ArrayList<News>(); private List<News> list = new ArrayList<News>(); private ListPopupWindowAdapter adapter_ = new ListPopupWindowAdapter(); private NewsViewModel newsViewModel; public ListFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.fragment_list, container, false); viewPager2 = inflate.findViewById(R.id.vp_news); tabLayout = inflate.findViewById(R.id.tab); mEditText = inflate.findViewById(R.id.word); adapter_.setEditText(mEditText); imageView = inflate.findViewById(R.id.iv_search); adapter_.setImageView(imageView); return inflate; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); sentUrlTask = new ListFragment.SentUrlTask("http://192.168.1.17:8080/NewsApi/news?method=allnews"); // sentUrlTask = new ListFragment.SentUrlTask("http://192.168.43.243:8080/NewsApi/news?method=allnews"); sentUrlTask.execute(); searchTask = new SearchTask(); mListPop = new ListPopupWindow(requireContext()); adapter_.setmListPop(mListPop); mListPop.setAdapter(adapter_); mListPop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); mListPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); mListPop.setAnchorView(mEditText);//设置ListPopupWindow的锚点,即关联PopupWindow的显示位置和这个锚点 mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String url = "http://192.168.1.17:8080/NewsApi/news?method=search&word=重"; url += s.toString(); searchTask.setUrl(url); } @Override public void afterTextChanged(Editable s) { mListPop.show(); } }); mEditText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // mListPop.show(); } }); TabAdapter tabAdapter = new TabAdapter(requireActivity()); data.add(new tuijianFragment()); data.add(new yvreFragment()); data.add(new caijingFragment()); data.add(new tiyvFragment()); data.add(new junshiFragment()); data.add(new kejiFragment()); data.add(new shoujiFragment()); data.add(new shumaFragment()); data.add(new shishangFragment()); data.add(new youxiFragment()); data.add(new jiaoyvFragment()); data.add(new jiankangFragment()); data.add(new lvyouFragment()); tabAdapter.setData(data); viewPager2.setAdapter(tabAdapter); tabLayout.setTabTextColors(Color.parseColor("#111111"),Color.parseColor("#0371DD")); new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() { @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { switch (position){ case 0: tab.setText("推荐"); break; case 1: tab.setText("娱乐"); break; case 2: tab.setText("财经"); break; case 3: tab.setText("体育"); break; case 4: tab.setText("军事"); break; case 5: tab.setText("科技"); break; case 6: tab.setText("手机"); break; case 7: tab.setText("数码"); break; case 8: tab.setText("时尚"); break; case 9: tab.setText("游戏"); break; case 10: tab.setText("教育"); break; case 11: tab.setText("健康"); break; case 12: tab.setText("旅游"); break; default: tab.setText("null"+position); } } }).attach(); } @Override public void clickImg(int index) { Gson gson = new Gson(); String s = ""; if (list.get(index).getHistory()!=true){ list.get(index).setHistory(true); s = gson.toJson(list.get(index)); newsViewModel.insertNews(gson.fromJson(s, New_.class)); } s = gson.toJson(list.get(index)); Intent intent = new Intent(requireContext(), NewsActivity.class); intent.putExtra("news",s); startActivity(intent); } private class SearchTask extends AsyncTask<Void,Void,String> { private String url; public void setUrl(String url) { this.url = url; } @Override protected String doInBackground(Void... voids) { return HttpUtil.setUrl(url); } @Override protected void onPostExecute(@NonNull String s) { super.onPostExecute(s); Gson gson = new Gson(); lists = gson.fromJson(s,new TypeToken<List<News>>(){}.getType()); adapter_.setList(lists); } } private class SentUrlTask extends AsyncTask<Void,Void,String> { private String url; public SentUrlTask(String url) { this.url = url; } @Override protected String doInBackground(Void... voids) { return HttpUtil.setUrl(url); } @Override protected void onPostExecute(@NonNull String s) { super.onPostExecute(s); Gson gson = new Gson(); System.out.println(s); list = gson.fromJson(s, new TypeToken<List<News>>(){}.getType()); for (int i = 0; i < 5; i++) { lists.add(list.get(i)); adapter_.setList(lists); } } } @Override public void onDestroy() { super.onDestroy(); if (sentUrlTask != null) { sentUrlTask.cancel(true); } } }
2、list_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" tools:context="com.me.fragment.ListFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#F00808" android:orientation="horizontal"> <EditText android:id="@+id/word" android:layout_width="340dp" android:layout_height="40dp" android:layout_marginStart="20dp" android:layout_marginTop="10dp" android:layout_marginEnd="5dp" android:layout_marginBottom="10dp" android:background="@drawable/ev" android:ems="18" android:hint="新闻关键字" android:inputType="textPersonName" android:textColorHint="#C0C0C0" android:textSize="18dp" /> <ImageView android:id="@+id/iv_search" android:layout_width="30dp" android:layout_height="20dp" android:layout_marginTop="20dp" android:src="@mipmap/search" /> </LinearLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tab" android:layout_width="match_parent" app:tabTextAppearance="@style/TabLayoutTextStyle" app:tabMode="scrollable" android:layout_height="60dp"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/TabLayoutTextStyle" android:text="Monday" /> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tuesday" /> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Wednesday" /> </com.google.android.material.tabs.TabLayout> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/vp_news" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>