當(dāng)前位置:
首頁(yè) >
(仿头条APP项目)6.点击过的新闻列表文字变灰和下拉刷新与滚动加载新闻数据
發(fā)布時(shí)間:2025/3/20
23
豆豆
生活随笔
收集整理的這篇文章主要介紹了
(仿头条APP项目)6.点击过的新闻列表文字变灰和下拉刷新与滚动加载新闻数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一.點(diǎn)擊過(guò)的新聞列表文字變灰
- 效果圖
- 實(shí)現(xiàn)思路
- 導(dǎo)入ormlite數(shù)據(jù)庫(kù)類依賴
- 利用ormlite創(chuàng)建數(shù)據(jù)庫(kù)和表
- 創(chuàng)建數(shù)據(jù)庫(kù)類MyDbHelper
- 創(chuàng)建數(shù)據(jù)庫(kù)中的新聞實(shí)體類NewInfo
- 頁(yè)面事務(wù)代碼編寫
- 列表點(diǎn)擊事件
- 在getView中判斷列表內(nèi)容是否被點(diǎn)擊過(guò)
- 創(chuàng)建queryIdInDb方法查詢當(dāng)前新聞是否存在數(shù)據(jù)庫(kù)中
- 根據(jù)是否存在數(shù)據(jù)庫(kù)表來(lái)改變標(biāo)題與日期的顏色
- 二.下拉刷新與滾動(dòng)加載新聞數(shù)據(jù)
- 效果圖
- 實(shí)現(xiàn)思路
- MyApi中添加獲取下一頁(yè)url地址的類
- 頁(yè)面事務(wù)代碼編寫
- 添加滾動(dòng)與下拉刷新器
- 下拉刷新
- 滾動(dòng)加載
- 總代碼
一.點(diǎn)擊過(guò)的新聞列表文字變灰
效果圖
實(shí)現(xiàn)思路
- 創(chuàng)建列表點(diǎn)擊事件利用數(shù)據(jù)庫(kù)存儲(chǔ)點(diǎn)擊過(guò)的新聞數(shù)據(jù)ID,通過(guò)判斷數(shù)據(jù)庫(kù)中是否有相應(yīng)的新聞數(shù)據(jù)ID來(lái)決定文字變灰(已查看)。
導(dǎo)入ormlite數(shù)據(jù)庫(kù)類依賴
implementation ‘com.j256.ormlite:ormlite-android:5.0’
利用ormlite創(chuàng)建數(shù)據(jù)庫(kù)和表
創(chuàng)建數(shù)據(jù)庫(kù)類MyDbHelper
import android.content.Context; import android.database.sqlite.SQLiteDatabase;import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils;import java.sql.SQLException;public class MyDbHelper extends OrmLiteSqliteOpenHelper {//創(chuàng)建數(shù)據(jù)庫(kù)public MyDbHelper(Context context) {super(context, "new", null, 1);}@Overridepublic void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {//創(chuàng)建表try {TableUtils.createTable(connectionSource,NewInfo.class);} catch (SQLException e) {e.printStackTrace();}}@Overridepublic void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {} }創(chuàng)建數(shù)據(jù)庫(kù)中的新聞實(shí)體類NewInfo
import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable;//定義數(shù)據(jù)庫(kù)中的新聞實(shí)體類 @DatabaseTable(tableName = "news_table") public class NewInfo {@DatabaseField(columnName = "id",generatedId = true)public Integer id;@DatabaseField(columnName = "newsId")public Integer newsId;//在表中有這個(gè)newId,表示已被讀過(guò),否則未讀public NewInfo(Integer newsId) {//自己調(diào)用,newsId為NewListData的新聞idthis.newsId = newsId;}public NewInfo() {//框架調(diào)用}@Overridepublic String toString() {return "NewInfo{" +"id=" + id +", newId=" + newsId +'}';} }頁(yè)面事務(wù)代碼編寫
列表點(diǎn)擊事件
//9.監(jiān)聽(tīng)列表點(diǎn)擊private void onliseners() {//9.1點(diǎn)擊監(jiān)聽(tīng)器pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//由于pullToRefreshListView本身算一條,所以從1開(kāi)始NewListData.DataBean.NewsBean bean = adapter.getListData().get(position-1); // Toast.makeText(getContext(),"id="+bean.id,Toast.LENGTH_SHORT).show();//9.2使用數(shù)據(jù)庫(kù)保存起來(lái)MyDbHelper myDbHelper = new MyDbHelper(getContext());Dao<NewInfo, Integer> dao = null;try {//dao是有增刪改查方法的對(duì)象dao = myDbHelper.getDao(NewInfo.class);List<NewInfo> list = dao.queryForEq("newsId",bean.id);//select語(yǔ)句if(list == null||list.size()==0) {//執(zhí)行一條insert語(yǔ)句dao.create(new NewInfo(bean.id));}System.out.println(dao.queryForAll().toString());//9.6調(diào)用adapter的notifyDataSetChanged方法更新數(shù)據(jù)adapter.notifyDataSetChanged();} catch (SQLException e) {e.printStackTrace();}}});}在getView中判斷列表內(nèi)容是否被點(diǎn)擊過(guò)
//判斷列表內(nèi)容是否被點(diǎn)擊過(guò)boolean isExist = false;try {isExist = queryIdInDb(newsBean.id);} catch (SQLException e) {e.printStackTrace();}if(type==0){//一圖//添加isExist參數(shù)判斷是否被點(diǎn)擊過(guò)return setDataToOneView(convertView, newsBean,isExist);}else {//三圖return setDataToThreeView(convertView, newsBean,isExist);}創(chuàng)建queryIdInDb方法查詢當(dāng)前新聞是否存在數(shù)據(jù)庫(kù)中
//9.3查詢當(dāng)前新聞是否存在數(shù)據(jù)庫(kù)中MyDbHelper myDbHelper;Dao<NewInfo,Integer> dao;private boolean queryIdInDb(int id) throws SQLException {if (myDbHelper == null){myDbHelper= new MyDbHelper(getContext());dao = myDbHelper.getDao(NewInfo.class);}List<NewInfo> list = dao.queryForEq("newsId",id);if(list == null ||list.size()==0){return false;//表示不存在數(shù)據(jù)庫(kù)中,沒(méi)有點(diǎn)擊過(guò)}else {return true;}}根據(jù)是否存在數(shù)據(jù)庫(kù)表來(lái)改變標(biāo)題與日期的顏色
holderOne.date.setTextColor(isExist? Color.GRAY : Color.BLACK);holderOne.title.setTextColor(isExist? Color.GRAY : Color.BLACK);二.下拉刷新與滾動(dòng)加載新聞數(shù)據(jù)
效果圖
實(shí)現(xiàn)思路
- 下拉刷新:1.清空集合。2.添加數(shù)據(jù)。3.刷新列表。4.關(guān)閉等待。
- 滾動(dòng)加載:1.不清空集合。2.添加數(shù)據(jù)。3.刷新列表。4.關(guān)閉等待。
MyApi中添加獲取下一頁(yè)url地址的類
public interface MyApi {//注解與方法組成實(shí)現(xiàn)類@GET("home.json")public Call<ResponseData> getType();@GET("10007/list_1.json")public Call<NewListData> getNewList();//獲取下一頁(yè)url地址@GETpublic Call<NewListData> getMoreData( @Url String loadMoreUrl);//將baseurl地址和變量組成新地址 }頁(yè)面事務(wù)代碼編寫
添加滾動(dòng)與下拉刷新器
//10.下拉刷新與滾動(dòng)加載private void setDataToView(NewListData.DataBean data) {//10.1判斷控件是否為空,為空才初始化if(pullToRefreshListView == null){//查找空間pullToRefreshListView = fragmentView.findViewById(R.id.pull_listview);//賦值一個(gè)適配器adapter = new NewListAdapter(data.news);pullToRefreshListView.setAdapter(adapter);//列表點(diǎn)擊變灰事件onliseners();//10.2給列表添加滾動(dòng)與下拉的刷新監(jiān)聽(tīng)器onlistenerspull(pullToRefreshListView);}}下拉刷新
@Overridepublic void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {//下拉//請(qǐng)求成功retrofit.create(MyApi.class).getNewList().enqueue(new Callback<NewListData>() {@Overridepublic void onResponse(Call<NewListData> call, Response<NewListData> response) {//10.5保存下一頁(yè)的地址loadMoreUrl = response.body().data.more;//10.4:請(qǐng)求到服務(wù)端數(shù)據(jù)后,先清空集合,再添加數(shù)據(jù),再刷新列表,關(guān)閉等待if(adapter!=null){adapter.getListData().clear();//清空adapter.getListData().addAll(response.body().data.news);adapter.notifyDataSetChanged();//刷新//結(jié)束刷新refreshView.onRefreshComplete();Toast.makeText(getContext(),"已刷新頁(yè)面",Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<NewListData> call, Throwable t) {}});滾動(dòng)加載
@Overridepublic void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {//滾動(dòng)//10.4 滾動(dòng)加載需要使用loadMoreUrl地址區(qū)的下一頁(yè)數(shù)據(jù)retrofit.create(MyApi.class).getMoreData(loadMoreUrl).enqueue(new Callback<NewListData>() {@Overridepublic void onResponse(Call<NewListData> call, Response<NewListData> response) {if(adapter!=null){adapter.getListData().addAll(response.body().data.news);adapter.notifyDataSetChanged();refreshView.onRefreshComplete();Toast.makeText(getContext(),"已顯示下一頁(yè)數(shù)據(jù)",Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<NewListData> call, Throwable t) {}});總代碼
import android.graphics.Color; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;import com.bumptech.glide.Glide; import com.example.administrator.zhjrtt.R; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.j256.ormlite.dao.Dao; import com.xzit.bean.NewListData; import com.xzit.db.MyDbHelper; import com.xzit.db.NewInfo; import com.xzit.fragment.BaseFragment; import com.xzit.net.MyApi;import java.sql.SQLException; import java.util.List;import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory;//1.繼承BaseFragment public class Page1Fragment extends BaseFragment {// 10.5 定義一個(gè)變量保存下一頁(yè)的url地址private String loadMoreUrl= null;// 2.重寫getMyView//3.布局列表View fragmentView;@Overrideprotected View getMyView() {//4.加載布局fragmentView = View.inflate(getContext(), R.layout.fragment_new_list,null);return fragmentView;}//5.請(qǐng)求服務(wù)端數(shù)據(jù)@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);//6.執(zhí)行請(qǐng)求retrofit.create(MyApi.class).getNewList().enqueue(new Callback<NewListData>() {@Overridepublic void onResponse(Call<NewListData> call, Response<NewListData> response) {//將請(qǐng)求的數(shù)據(jù)傳給視圖setDataToView(response.body().data);//10.5保存下一頁(yè)的地址loadMoreUrl = response.body().data.more;}@Overridepublic void onFailure(Call<NewListData> call, Throwable t) {}});}//7.顯示PullToRefreshListView pullToRefreshListView;NewListAdapter adapter;//10.下拉刷新與滾動(dòng)加載private void setDataToView(NewListData.DataBean data) {//10.1判斷控件是否為空,為空才初始化if(pullToRefreshListView == null){//查找空間pullToRefreshListView = fragmentView.findViewById(R.id.pull_listview);//賦值一個(gè)適配器adapter = new NewListAdapter(data.news);pullToRefreshListView.setAdapter(adapter);//列表點(diǎn)擊變灰事件onliseners();//10.2給列表添加滾動(dòng)與下拉的刷新監(jiān)聽(tīng)器onlistenerspull(pullToRefreshListView);}}private void onlistenerspull(PullToRefreshListView pullToRefreshListView) {//10.3設(shè)置模式為BOTHpullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {//下拉//請(qǐng)求成功retrofit.create(MyApi.class).getNewList().enqueue(new Callback<NewListData>() {@Overridepublic void onResponse(Call<NewListData> call, Response<NewListData> response) {//10.5保存下一頁(yè)的地址loadMoreUrl = response.body().data.more;//10.4:請(qǐng)求到服務(wù)端數(shù)據(jù)后,先清空集合,再添加數(shù)據(jù),再刷新列表,關(guān)閉等待if(adapter!=null){adapter.getListData().clear();//清空adapter.getListData().addAll(response.body().data.news);adapter.notifyDataSetChanged();//刷新//結(jié)束刷新refreshView.onRefreshComplete();Toast.makeText(getContext(),"已刷新頁(yè)面",Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<NewListData> call, Throwable t) {}});}@Overridepublic void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {//滾動(dòng)//10.4 滾動(dòng)加載需要使用loadMoreUrl地址區(qū)的下一頁(yè)數(shù)據(jù)retrofit.create(MyApi.class).getMoreData(loadMoreUrl).enqueue(new Callback<NewListData>() {@Overridepublic void onResponse(Call<NewListData> call, Response<NewListData> response) {if(adapter!=null){adapter.getListData().addAll(response.body().data.news);adapter.notifyDataSetChanged();refreshView.onRefreshComplete();Toast.makeText(getContext(),"已顯示下一頁(yè)數(shù)據(jù)",Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<NewListData> call, Throwable t) {}});}});}//9.監(jiān)聽(tīng)列表點(diǎn)擊private void onliseners() {//9.1點(diǎn)擊監(jiān)聽(tīng)器pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//由于pullToRefreshListView本身算一條,所以從1開(kāi)始NewListData.DataBean.NewsBean bean = adapter.getListData().get(position-1); // Toast.makeText(getContext(),"id="+bean.id,Toast.LENGTH_SHORT).show();//9.2使用數(shù)據(jù)庫(kù)保存起來(lái)MyDbHelper myDbHelper = new MyDbHelper(getContext());Dao<NewInfo, Integer> dao = null;try {//dao是有增刪改查方法的對(duì)象dao = myDbHelper.getDao(NewInfo.class);List<NewInfo> list = dao.queryForEq("newsId",bean.id);//select語(yǔ)句if(list == null||list.size()==0) {//執(zhí)行一條insert語(yǔ)句dao.create(new NewInfo(bean.id));}System.out.println(dao.queryForAll().toString());//9.6調(diào)用adapter的notifyDataSetChanged方法更新數(shù)據(jù)adapter.notifyDataSetChanged();} catch (SQLException e) {e.printStackTrace();}}});}class ViewHolderOne{public TextView title;public TextView date;public ImageView image;}class ViewHolderThree extends ViewHolderOne{public ImageView image1;public ImageView image2;}//8.定義適配器class NewListAdapter extends BaseAdapter {private List<NewListData.DataBean.NewsBean> listData;public List<NewListData.DataBean.NewsBean> getListData() {return listData;}public NewListAdapter(List<NewListData.DataBean.NewsBean> list) {listData = list;}@Overridepublic int getViewTypeCount() {//返回兩種視圖,一種是一圖的,一種是三圖的return 2;}@Overridepublic int getItemViewType(int position) {NewListData.DataBean.NewsBean bean = listData.get(position);if(bean.type==0){//一圖return 0;//R.layout.item_new_one.xml}else {//三圖return 1;R.layout.item_new_three.xml}}@Overridepublic int getCount() {return listData.size();}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolderOne holderOne = null;ViewHolderThree holderThree = null;//獲取數(shù)據(jù)NewListData.DataBean.NewsBean newsBean = listData.get(position);//判斷當(dāng)前的視圖是一圖還是三圖int type = getItemViewType(position);//判斷列表內(nèi)容是否被點(diǎn)擊過(guò)boolean isExist = false;try {isExist = queryIdInDb(newsBean.id);} catch (SQLException e) {e.printStackTrace();}if(type==0){//一圖return setDataToOneView(convertView, newsBean,isExist);}else {//三圖return setDataToThreeView(convertView, newsBean,isExist);}}@NonNullprivate View setDataToThreeView(View convertView, NewListData.DataBean.NewsBean newsBean, boolean isExist) {ViewHolderThree holderThree;if(convertView == null){convertView = View.inflate(getContext(),R.layout.item_new_three,null);holderThree = new ViewHolderThree();holderThree.title = convertView.findViewById(R.id.item_title_three);holderThree.date = convertView.findViewById(R.id.item_date_three);holderThree.image = convertView.findViewById(R.id.item_image1_three);holderThree.image1 = convertView.findViewById(R.id.item_image2_three);holderThree.image2 = convertView.findViewById(R.id.item_image3_three);convertView.setTag(holderThree);}else {holderThree = (ViewHolderThree) convertView.getTag();}holderThree.title.setText(newsBean.title);holderThree.date.setText(newsBean.pubdate);String imageUrl= "http://192.168.31.114:8080"+newsBean.listimage;String imageUrl1= "http://192.168.31.114:8080"+newsBean.listimage1;String imageUrl2= "http://192.168.31.114:8080"+newsBean.listimage2;Glide.with(getContext()).load(imageUrl).into(holderThree.image);Glide.with(getContext()).load(imageUrl1).into(holderThree.image1);Glide.with(getContext()).load(imageUrl2).into(holderThree.image2);//9.4根據(jù)是否存在數(shù)據(jù)庫(kù)表來(lái)改變標(biāo)題與日期的顏色holderThree.date.setTextColor(isExist? Color.GRAY : Color.BLACK);holderThree.title.setTextColor(isExist? Color.GRAY : Color.BLACK);return convertView;}//當(dāng)視圖是一張圖片時(shí)@NonNullprivate View setDataToOneView(View convertView, NewListData.DataBean.NewsBean newsBean, boolean isExist) {ViewHolderOne holderOne;if(convertView ==null){//視圖不是重用時(shí)convertView = View.inflate(getContext(),R.layout.item_new_one,null);holderOne = new ViewHolderOne();holderOne. title = convertView.findViewById(R.id.item_title);holderOne. date = convertView.findViewById(R.id.item_date);holderOne. image = convertView.findViewById(R.id.item_image);convertView.setTag(holderOne);}else {//視圖重用holderOne= (ViewHolderOne) convertView.getTag();}holderOne. title.setText(newsBean.title);holderOne. date.setText(newsBean.pubdate);String imageUrl= "http://192.168.31.114:8080"+newsBean.listimage;Glide.with(getContext()).load(imageUrl).into(holderOne.image);//9.4根據(jù)是否存在數(shù)據(jù)庫(kù)表來(lái)改變標(biāo)題與日期的顏色holderOne.date.setTextColor(isExist? Color.GRAY : Color.BLACK);holderOne.title.setTextColor(isExist? Color.GRAY : Color.BLACK);return convertView;}}//9.3查詢當(dāng)前新聞是否存在數(shù)據(jù)庫(kù)中MyDbHelper myDbHelper;Dao<NewInfo,Integer> dao;private boolean queryIdInDb(int id) throws SQLException {if (myDbHelper == null){myDbHelper= new MyDbHelper(getContext());dao = myDbHelper.getDao(NewInfo.class);}List<NewInfo> list = dao.queryForEq("newsId",id);if(list == null ||list.size()==0){return false;//表示不存在數(shù)據(jù)庫(kù)中,沒(méi)有點(diǎn)擊過(guò)}else {return true;}} }總結(jié)
以上是生活随笔為你收集整理的(仿头条APP项目)6.点击过的新闻列表文字变灰和下拉刷新与滚动加载新闻数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: (仿头条APP项目)5.列表页面设计实现
- 下一篇: (仿头条APP项目)7.首页标签页完善和