Android 如何在Recyclerview中制作更多节目?
我正在开发一个应用程序,其中有RecyclerView,我要做的是,当用户滚动recycler视图时,会向他们显示一个show more按钮,并单击按钮使用json从服务器加载更多数据,请帮助我Android 如何在Recyclerview中制作更多节目?,android,Android,我正在开发一个应用程序,其中有RecyclerView,我要做的是,当用户滚动recycler视图时,会向他们显示一个show more按钮,并单击按钮使用json从服务器加载更多数据,请帮助我 public class CDealAppListing extends Fragment { public static String m_DealListingURL = "http://192.168.0.110:8080/ireward/rest/json/metallica/getDe
public class CDealAppListing extends Fragment {
public static String m_DealListingURL = "http://192.168.0.110:8080/ireward/rest/json/metallica/getDealListInJSON";
public static String s_szresult = " ";
public ArrayList<CDealAppDatastorage> m_oDataList;
public int[] m_n_FormImage;
public View m_Main;
public CRegistrationSessionManagement m_oSessionManagement;
public String m_szMobileNumber, m_szEncryptedPassword;
private RecyclerView m_RecyclerView;
private RecyclerView.Adapter m_oAdapter;
private CJsonsResponse m_oJsonsResponse;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
m_Main = inflater.inflate(R.layout.deal_app_listing, container, false);//intialize mainLayout
new CDealDataSent().execute(m_DealListingURL);
init();//initialize method
return m_Main;
}
public void init() {
m_n_FormImage = new int[]{
R.drawable.amazon,
R.drawable.whatsapp,
R.drawable.zorpia,
R.drawable.path,
R.drawable.app_me,
R.drawable.evernote,
R.drawable.app_me};
m_RecyclerView = (RecyclerView) m_Main.findViewById(R.id.my_recycler_view);//finding id of recyclerview
m_RecyclerView.setItemAnimator(new DefaultItemAnimator());//setting default animation to recyclerview
m_RecyclerView.setHasFixedSize(true);//fixing size of recyclerview
//Layout manager for Recycler view
m_RecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//showing odata vertically to user.
// m_RecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1));
m_oSessionManagement = new CRegistrationSessionManagement(getActivity());
HashMap<String, String> user = m_oSessionManagement.getRegistrationDetails();
m_szEncryptedPassword = user.get(m_oSessionManagement.s_szKEY_PASSWORD);
m_szMobileNumber = user.get(m_oSessionManagement.s_szKEY_MOBILENUMBER);
}
//sending deal data to retreive response from server
public String DealListing(String url, CRegistrationDataStorage login) {
InputStream inputStream = null;
m_oJsonsResponse = new CJsonsResponse();
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("agentCode", "99999999");
jsonObject.put("pin", "08556CB2450231B0D7235C3446B078A6");
jsonObject.put("recordcount", "5");
jsonObject.put("lastcountvalue", "0");
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
// 9. receive response as inputStream
inputStream = entity.getContent();
System.out.println("InputStream....:" + inputStream.toString());
System.out.println("Response....:" + httpResponse.toString());
StatusLine statusLine = httpResponse.getStatusLine();
System.out.println("statusLine......:" + statusLine.toString());
////Log.d("resp_body", resp_body.toString());
int statusCode = statusLine.getStatusCode();
// 10. convert inputstream to string
if (statusCode == 200) {
// 10. convert inputstream to string
if (inputStream != null)
s_szresult = m_oJsonsResponse.convertInputStreamToString(inputStream);
//String resp_body =
EntityUtils.toString(httpResponse.getEntity());
} else
s_szresult = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
System.out.println("resul.....:" + s_szresult);
// 11. return s_szResult
return s_szresult;
}
// sending deal data to server and retreive response......
class CDealDataSent extends AsyncTask<String, Void, String> {
public JSONObject m_oResponseobject;
public ProgressDialog m_PDialog;
public CRegistrationDataStorage oRegisterStorage;
public CDealAppDatastorage item;
// @Override
protected void onPreExecute() {
super.onPreExecute();
m_PDialog = new ProgressDialog(getActivity());
m_PDialog.setMessage("Please wait while Loading Deals...");
m_PDialog.setCancelable(false);
m_PDialog.show();
}
@Override
protected String doInBackground(String... urls) {
return DealListing(urls[0], oRegisterStorage);// sending data to server...
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
m_PDialog.dismiss();
try {
m_oResponseobject = new JSONObject(result);// getting response from server
final JSONArray posts = m_oResponseobject.optJSONArray("dealList");
m_oDataList = new ArrayList<CDealAppDatastorage>();
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.getJSONObject(i);
item = new CDealAppDatastorage();
item.setM_szHeaderText(post.getString("dealname"));
item.setM_szsubHeaderText(post.getString("dealcode"));
item.setM_n_Image(m_n_FormImage[i]);
m_oDataList.add(item);
}
if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {
m_oAdapter = new CDealAppListingAdapter(m_oDataList);//creating object of adapter and addd setting odata to adapter for use.
m_RecyclerView.setAdapter(m_oAdapter);//adding adapter to recyclerview
} else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {
Toast.makeText(getActivity(), "Connection not avaliable", Toast.LENGTH_SHORT).show();
}
System.out.println("agentCode...." + m_szMobileNumber);
System.out.println("password...." + m_szEncryptedPassword);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public类CDealAppListing扩展片段{
公共静态字符串m_DealListingURL=”http://192.168.0.110:8080/ireward/rest/json/metallica/getDealListInJSON";
公共静态字符串s_szresult=“”;
公共ArrayList m_oDataList;
公共int[]m_n__FormImage;
公众视野;
公共注册;会话管理;会话管理;
公共字符串m_szMobileNumber,m_szEncryptedPassword;
私人回收视图m_RecyclerView;
专用回收器查看适配器m_oAdapter;
私人CJsonsResponse m_OJSONS Response;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
m_Main=inflater.inflate(R.layout.deal_app_listing,container,false);//初始化mainLayout
新建CDealDataSent().execute(m_DealListingURL);
init();//初始化方法
返回m_Main;
}
公共void init(){
m_n_FormImage=新整数[]{
R.drawable.amazon,
R.drawable.whatsapp,
R.drawable.zorpia,
R.drawable.path,
R.drawable.app\u me,
R.drawable.evernote,
R.drawable.app_me};
m_RecyclerView=(RecyclerView)m_Main.findViewById(R.id.my_RecyclerView);//查找RecyclerView的id
m_RecyclerView.setItemAnimator(新的DefaultItemAnimator());//将默认动画设置为RecyclerView
m_RecyclerView.setHasFixedSize(true);//固定RecyclerView的大小
//回收器视图的布局管理器
m_RecyclerView.setLayoutManager(新的LinearLayoutManager(getActivity());//向用户垂直显示odata。
//m_RecyclerView.setLayoutManager(新的StaggedGridLayoutManager(2,1));
m_oSessionManagement=新注册会话管理(getActivity());
HashMap user=m_oSessionManagement.getRegistrationDetails();
m_szEncryptedPassword=user.get(m_oSessionManagement.s_szKEY_PASSWORD);
m_szMobileNumber=user.get(m_oSessionManagement.s_szKEY_MOBILENUMBER);
}
//正在发送交易数据以从服务器检索响应
公共字符串DealListing(字符串url、凭据数据存储登录){
InputStream InputStream=null;
m_oJsonsResponse=新的CJsonsResponse();
试一试{
//1.创建HttpClient
HttpClient HttpClient=新的DefaultHttpClient();
//2.向给定URL发出POST请求
HttpPost HttpPost=新的HttpPost(url);
字符串json=“”;
//3.构建jsonObject
JSONObject JSONObject=新的JSONObject();
jsonObject.put(“代理代码”、“9999999”);
jsonObject.put(“引脚”,“08556CB2450231B0D7235C3446B078A6”);
jsonObject.put(“记录计数”,“5”);
jsonObject.put(“lastcountvalue”,“0”);
//4.将JSONObject转换为JSON转换为字符串
json=jsonObject.toString();
//5.将json设置为StringEntity
StringEntity se=新的StringEntity(json);
//6.设置httpPost实体
httpPost.setEntity(se);
//7.设置一些标题以通知服务器内容的类型
setHeader(“内容类型”、“应用程序/json”);
//8.对给定URL执行POST请求
HttpResponse HttpResponse=httpclient.execute(httpPost);
HttpEntity entity=httpResponse.getEntity();
//9.将响应作为inputStream接收
inputStream=entity.getContent();
System.out.println(“InputStream…”:“+InputStream.toString());
System.out.println(“响应:”+httpResponse.toString());
StatusLine StatusLine=httpResponse.getStatusLine();
System.out.println(“statusLine……”:“+statusLine.toString());
////Log.d(“resp_body”,resp_body.toString());
int statusCode=statusLine.getStatusCode();
//10.将inputstream转换为字符串
如果(状态代码==200){
//10.将inputstream转换为字符串
如果(inputStream!=null)
s_szresult=m_oJsonsResponse.convertInputStreamToString(inputStream);
//字符串响应体=
toString(httpResponse.getEntity());
}否则
s_szresult=“不起作用!”;
}捕获(例外e){
d(“InputStream”,例如getLocalizedMessage());
}
System.out.println(“结果…:”+s_szresult);
//11.返回s_szResult
返回s_szresult;
}
//正在将交易数据发送到服务器并检索响应。。。。。。
类CDealDataSent扩展了AsyncTask{
公共JSONObject m_或响应对象;
公共进步对话;
公共凭证登记数据存储或登记存储;
公共CDealAppDatastorage项;
//@覆盖
受保护的void onPreExecute(){
super.onPreExecute();
m_PDialog=newprogressdialog(getActivity());
m_PDialog.setMessage(“加载交易时请稍候…”);
m_PDialog.setCancelable(假);
m_PDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
返回DealListing(URL[0],oRegisterStorage);//正在向服务器发送数据。。。
}
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(字符串结果){
m_PDialog.discouse();
试一试{
m_oResponseobject=new JSONObject(result);//从服务器获取响应
最终JSON