SuperBase
package com.example.smartcity.adpter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class SuperBase extends BaseAdapter {
public SuperBase(int mList) {
this.mList = mList;
}
private int mList;
@Override
public int getCount() {
return mList;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public abstract View getView(int position, View convertView, ViewGroup parent);
}
SuperBases(选中item更换样式)
package com.example.smartcity.adpter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class SuperBases extends BaseAdapter {
public SuperBases(int mList) {
this.mList = mList;
}
private int mList;
@Override
public int getCount() {
return mList;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public abstract View getView(int position, View convertView, ViewGroup parent);
public abstract void setSelectItem(int position);
}
SuperPage
package com.example.smartcity.adpter;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
public abstract class SuperPage extends PagerAdapter {
private int mList;
public SuperPage(int mList) {
this.mList = mList;
}
@Override
public int getCount() {
return mList;
}
@NonNull
@Override
public abstract Object instantiateItem(@NonNull ViewGroup container, int position);
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
SuperPages(有圆圈标识)
package com.example.smartcity.adpter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.example.smartcity.R;
public abstract class SuperPages extends PagerAdapter {
private int mList;
private RadioGroup mCircle;
private Context context;
public SuperPages(int mList, RadioGroup radioGroup, Context context) {
this.mList = mList;
this.mCircle = radioGroup;
this.context = context;
setCircle();
}
public void setCircle(){
for (int i = 0; i < mList; i++) {
RadioButton radioButton = new RadioButton(context);
radioButton.setButtonDrawable(R.drawable.circle_select);
radioButton.setId(i);
mCircle.addView(radioButton);
}
}
@Override
public int getCount() {
return mList;
}
@NonNull
@Override
public abstract Object instantiateItem(@NonNull ViewGroup container, int position);
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
NewsAdapter
package com.example.smartcity.adpter;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
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 androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.example.smartcity.R;
import com.example.smartcity.activity.main.NewsInfoActivity;
import com.example.smartcity.bean.NewsInfoSkip;
import com.example.smartcity.info.NewsInfo;
import com.example.smartcity.info.NewsType;
import com.example.smartcity.utils.GetNetImage;
import com.example.smartcity.utils.KeyUtils;
import java.util.ArrayList;
import java.util.List;
import static com.example.smartcity.utils.KeyUtils.NEWS;
public class NewsAdapter extends PagerAdapter {
private Context context;
private List<NewsType.DataDTO> mTabs;
private List<NewsInfo.RowsDTO> mNewsInfo;
public NewsAdapter(Context context, List<NewsType.DataDTO> mTabs, List<NewsInfo.RowsDTO> mNewsInfo) {
this.context = context;
this.mTabs = mTabs;
this.mNewsInfo = mNewsInfo;
}
public CharSequence getPageTitle(int position){
return mTabs.get(position).getName();
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ListView listView = new ListView(context);
setListView(listView,position);
container.addView(listView);
return listView;
}
public void setListView(ListView listView,int position){
ArrayList<NewsInfo.RowsDTO> newsInfos = new ArrayList<>();
for (int i = 0; i < mNewsInfo.size(); i++) {
if (mNewsInfo.get(i).getType().equals(mTabs.get(position).getId()+"")){
newsInfos.add(mNewsInfo.get(i));
}
}
SuperBase superBase = new SuperBase(newsInfos.size()) {
private ImageView mNewsImg;
private TextView mNewsTitle;
private TextView mNewsContent;
private TextView mNewsMsg;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_news,parent,false);
mNewsImg = (ImageView) convertView.findViewById(R.id.news_img);
mNewsTitle = (TextView) convertView.findViewById(R.id.news_title);
mNewsContent = (TextView) convertView.findViewById(R.id.news_content);
mNewsMsg = (TextView) convertView.findViewById(R.id.news_msg);
new GetNetImage(context,newsInfos.get(position).getCover(),mNewsImg);
mNewsTitle.setText(newsInfos.get(position).getTitle());
mNewsContent.setText(newsInfos.get(position).getContent());
String msg = "评论总数:"+newsInfos.get(position).getCommentNum()+"\t\t发布时间:"+newsInfos.get(position).getPublishDate();
mNewsMsg.setText(msg);
return convertView;
}
};
listView.setAdapter(superBase);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsInfoSkip newsInfoSkip = new NewsInfoSkip();
NewsInfo.RowsDTO rowsDTO = newsInfos.get(position);
newsInfoSkip.setImage(rowsDTO.getCover());
newsInfoSkip.setTitle(rowsDTO.getTitle());
newsInfoSkip.setContent(rowsDTO.getContent());
Intent intent = new Intent(context, NewsInfoActivity.class);
intent.putExtra(NEWS,newsInfoSkip);
context.startActivity(intent);
}
});
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
LiveNewsAdapter(生活新闻)
package com.example.smartcity.adpter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.example.smartcity.R;
import com.example.smartcity.activity.main.NewsInfoActivity;
import com.example.smartcity.bean.NewsInfoSkip;
import com.example.smartcity.info.NewsInfo;
import com.example.smartcity.info.NewsType;
import com.example.smartcity.info.life.LiveNews;
import com.example.smartcity.info.life.LiveNewsType;
import com.example.smartcity.utils.GetNetImage;
import java.util.ArrayList;
import java.util.List;
import static com.example.smartcity.utils.KeyUtils.NEWS;
public class LiveNewsAdapter extends PagerAdapter {
private Context context;
private List<LiveNewsType.DataDTO> mTabs;
private List<LiveNews.RowsDTO> mNewsInfo;
public LiveNewsAdapter(Context context, List<LiveNewsType.DataDTO> mTabs, List<LiveNews.RowsDTO> mNewsInfo) {
this.context = context;
this.mTabs = mTabs;
this.mNewsInfo = mNewsInfo;
}
public CharSequence getPageTitle(int position){
return mTabs.get(position).getName();
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ListView listView = new ListView(context);
setListView(listView,position);
container.addView(listView);
return listView;
}
public void setListView(ListView listView,int position){
ArrayList<LiveNews.RowsDTO> newsInfos = new ArrayList<>();
for (int i = 0; i < mNewsInfo.size(); i++) {
if (mNewsInfo.get(i).getType().equals(mTabs.get(position).getId()+"")){
newsInfos.add(mNewsInfo.get(i));
}
}
SuperBase superBase = new SuperBase(newsInfos.size()) {
private ImageView mNewsImg;
private TextView mNewsTitle;
private TextView mNewsContent;
private TextView mNewsMsg;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_news,parent,false);
mNewsImg = (ImageView) convertView.findViewById(R.id.news_img);
mNewsTitle = (TextView) convertView.findViewById(R.id.news_title);
mNewsContent = (TextView) convertView.findViewById(R.id.news_content);
mNewsMsg = (TextView) convertView.findViewById(R.id.news_msg);
new GetNetImage(context,newsInfos.get(position).getCover(),mNewsImg);
mNewsTitle.setText(newsInfos.get(position).getTitle());
mNewsContent.setText(newsInfos.get(position).getContent());
String msg = "评论总数:"+newsInfos.get(position).getCommentNum()+"\t\t发布时间:"+newsInfos.get(position).getPublishDate();
mNewsMsg.setText(msg);
return convertView;
}
};
listView.setAdapter(superBase);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsInfoSkip newsInfoSkip = new NewsInfoSkip();
LiveNews.RowsDTO rowsDTO = newsInfos.get(position);
newsInfoSkip.setImage(rowsDTO.getCover());
newsInfoSkip.setTitle(rowsDTO.getTitle());
newsInfoSkip.setContent(rowsDTO.getContent());
Intent intent = new Intent(context, NewsInfoActivity.class);
intent.putExtra(NEWS,newsInfoSkip);
context.startActivity(intent);
}
});
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}