侧拉框+Xlist下拉加载数据+跳转activity

配置文件

(网络请求)

添加依赖 implementation 'com.android.support:support-v13:28.0.0' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:design:28.0.0'

布局文件
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:id="@+id/drawer"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">


xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".SendActivity">






创建Fragment
package com.example.moni3;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AFragment extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_a, container, false); }

}
package com.example.moni3;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(ImageConfs.getcon(this));
}
}
package com.example.moni3;
import android.content.Context;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
public class ImageConfs {
public static ImageLoaderConfiguration getcon(Context context){
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
.memoryCacheSizePercentage(10)
.diskCacheSize(5010241024)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.writeDebugLogs()
.build();
return configuration; }public static DisplayImageOptions getMan(Context context){ DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheOnDisk(true) .cacheInMemory(true).showImageOnLoading(R.mipmap.ic_launcher) .showImageOnFail(R.drawable.ic_launcher_background) .bitmapConfig(Bitmap.Config.RGB_565) .displayer(new CircleBitmapDisplayer()) .build(); return options; }

}
package com.example.moni3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
实现adapter
public class Madapter extends BaseAdapter {
List list; Context context; public void setDats(List datas){ list = datas; notifyDataSetChanged(); }public List getList() { return list; }public void addDats(List datas){ if(datas !=null){ list.addAll(datas); notifyDataSetChanged(); } }public Madapter(Context context) { this.context = context; list = new ArrayList(); }public void setList(List list){ this.list = list; this.list.addAll(list); notifyDataSetChanged(); }@Override public int getCount() { return list.size(); }@Override public Object getItem(int position) { return list.get(position); }@Override public long getItemId(int position) { return position; }@Override public View getView(int position, View convertView, ViewGroup parent) {ViewHolde vh; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.list_text,null); vh = new ViewHolde(convertView); }else{ vh = (ViewHolde) convertView.getTag(); } Person.ResultDate data = https://www.it610.com/article/(Person.ResultDate) getItem(position); vh.BindDate(data); return convertView; } class ViewHolde{ TextView t1; TextView t2; ImageView image; public ViewHolde(View view) { t1 = view.findViewById(R.id.t1); t2 = view.findViewById(R.id.t2); image = view.findViewById(R.id.image); view.setTag(this); }public void BindDate(Person.ResultDate data){ t1.setText(data.getName()); t2.setText(data.getSummary()); ImageLoader.getInstance().displayImage(data.getImageUrl(),image,ImageConfs.getMan(context)); } }

}
package com.example.moni3;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import com.example.xlist.me.maxwin.view.XListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends BaseActivity {
private int page=1; private XListView xlv; private Madapter ma; private DrawerLayout drawer; private TabLayout table; private ViewPager viewpager; private ArrayList sid; private ActionBarDrawerToggle toggle; @Override protected int getLayoutId() { return R.layout.activity_main; }private void canloadmore(boolean b) { xlv.setPullLoadEnable(b); }private void LoadDate(int page) { final String path = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page="+page+"&count=3"; String UrlWith = path+page; new AsyncTask>(){@Override protected List doInBackground(String... strings) { Person dian = null; try { URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setReadTimeout(5000); int request = httpURLConnection.getResponseCode(); if(request == 200){ String str = String2(httpURLConnection.getInputStream()); dian = new Gson().fromJson(str,Person.class); } } catch (Exception e) { e.printStackTrace(); } return dian == null ? null : dian.getResult(); }@Override protected void onPostExecute(List resultDates) { if(resultDates == null){ Toast.makeText(MainActivity.this,"请求数据失败",Toast.LENGTH_SHORT).show(); return; } updateData(resultDates); loade(); canloadmore(resultDates.size()==3); } }.execute(UrlWith); } private void loade(){ page++; xlv.stopLoadMore(); xlv.stopRefresh(); }private void updateData(List resultDates) { if(page == 1){ ma.setDats(resultDates); Date date = new Date(); xlv.setRefreshTime(date.toLocaleString()); }else{ ma.addDats(resultDates); }}private String String2(InputStream inputStream) throws IOException {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String text=null; while((text=bufferedReader.readLine())!=null){ stringBuilder.append(text); } bufferedReader.close(); return stringBuilder.toString(); }public void initViewBar() { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); toggle = new ActionBarDrawerToggle(this,drawer,R.string.open,R.string.close); toggle.syncState(); drawer.addDrawerListener(toggle); }@Override public boolean onOptionsItemSelected(MenuItem item) { if(toggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }public void init() { drawer = findViewById(R.id.drawer); table = findViewById(R.id.table); viewpager = findViewById(R.id.viewpager); drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED); xlv = findViewById(R.id.xlv); ma = new Madapter(this); xlv.setAdapter(ma); xlv.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { page = 1; LoadDate(page); }@Override public void onLoadMore() { LoadDate(page); } }); canloadmore(true); LoadDate(page); xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this,SendActivity.class); intent.putExtra("name",ma.getList().get(position-1).getImageUrl()); intent.putExtra("name1",ma.getList().get(position-1).getName()); startActivity(intent); } }); }

}
package com.example.moni3;
import java.util.List;
解析json
public class Person {
public List result;
public String message;
public int status;
public List getResult() { return result; }public void setResult(List result) { this.result = result; }public String getMessage() { return message; }public void setMessage(String message) { this.message = message; }public int getStatus() { return status; }public void setStatus(int status) { this.status = status; }public static class ResultDate{public int id; public String imageUrl; public String name; public int rank; public String summary; public int getId() { return id; }public void setId(int id) { this.id = id; }public String getImageUrl() { return imageUrl; }public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; }public String getName() { return name; }public void setName(String name) { this.name = name; }public int getRank() { return rank; }public void setRank(int rank) { this.rank = rank; }public String getSummary() { return summary; }public void setSummary(String summary) { this.summary = summary; } }

}
package com.example.moni3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SendActivity extends AppCompatActivity {
private TextView name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send); name = findViewById(R.id.name); Intentintent = getIntent(); String name1 = intent.getStringExtra("name"); this.name.setText(name1); }

}
进行BaseActivity的抽取
package com.example.moni3;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public abstract class BaseActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutId()); init(); initViewBar(); }protected abstract int getLayoutId(); protected abstract void init(); protected abstract void initViewBar();

}
携带数据进行跳转到另一个activity(创建新的activity)
package com.example.moni3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
public class SendActivity extends BaseActivity {
private TextView name; private ImageView image; @Override protected int getLayoutId() { return R.layout.activity_send; }@Override protected void init() {name = findViewById(R.id.name); image = findViewById(R.id.image); Intentintent = getIntent(); String name1 = intent.getStringExtra("name"); String namen = intent.getStringExtra("name1"); ImageLoader.getInstance().displayImage(name1,image,ImageConfs.getMan()); this.name.setText(namen); }@Override protected void initViewBar() {}

【侧拉框+Xlist下拉加载数据+跳转activity】}

    推荐阅读