如何在 RecycleVIew 中初始化适配器?
How to Initialize adapter in RecycleVIew?
在此之前,我使用的是 ListView,它工作正常。但我想将 ListView 设置为水平。所以我开始知道 RecycleView 比 listview 更可靠。
我正在尝试使用回收视图并收到类似
的错误
No adapter attached; skipping layout
这意味着问题出在我的适配器中class我不知道我的适配器中需要什么代码class我只是这样试过。
main.java
public class DynamicButon extends Fragment {
public DynamicButon(){};
JSONObject jsonobject;
JSONArray ownerObj;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
ListViewAdapterDynamic mAdapter;
ArrayList<HashMap<String, String>> arraylist;
String uid = "0";
SessionManager session;
private static String url_visitor = "";
JSONParser jParser = new JSONParser();
ArrayList<String> itemwod = new ArrayList<String>();
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_dynamic_buton, container, false);
getActivity().setTitle("Complete Order");
// listview = (ListView) view.findViewById(R.id.lvvisit);
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
uid = user.get(SessionManager.KEY_ID);
new DownloadJSON().execute();
return view;
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// avi.show();
}
@Override
protected Void doInBackground(Void... args) {
try {
arraylist = new ArrayList<HashMap<String, String>>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", uid));
JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);
int success1 = Integer.parseInt(json.getString("success4"));
Log.d("success4", json.toString());
if (success1 == 0) {
Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
}
if (success1 == 1) {
ownerObj = json.getJSONArray("cy");
for (int i = 0; i < ownerObj.length(); i++) {
jsonobject = ownerObj.getJSONObject(i);
item.add(jsonobject.getString("company"));
}
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void args) {
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.mylist);
mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ListViewAdapterDynamic(getActivity(), itemwod);
mRecyclerView.setAdapter(mAdapter);
}
}
}
ListViewAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by sachin on 3/15/2017.
*/
public class ListViewAdapterDynamic extends BaseAdapter {
Context cntx;
View view;
ArrayList<String> itemwod = new ArrayList<String>();
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapterDynamic(Context context,
ArrayList<String> o_parties
) {
// TODO Auto-generated constructor stub
cntx = context;
itemwod = o_parties;
}
@Override
public int getCount() {
return itemwod.size();
}
@Override
public Object getItem(int position) {
return itemwod.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
public View getView(final int position, View convertView, ViewGroup parent) {
Button cancel;
inflater = (LayoutInflater) cntx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(cntx);
convertView = inflater.inflate(R.layout.raw_dynamic, parent,
false);
cancel=(Button)convertView.findViewById(R.id.btn) ;
cancel.setText(itemwod.get(position));
return convertView;
}
}
像这样我尝试但没有在 ListViewAdapterDynamic 中工作我试图实现
RecyclerView.Adapter<MyViewHolder>
然后出现错误,这就是为什么我只是继续扩展 Baseadapter 我在列表视图中所做的。
使用 RecyclerView 设置列表项。
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);
使用回收视图。
在水平方向上使用 Recyclerview
按照这个 link https://developer.android.com/training/material/lists-cards.html
使用 RecyclerView
而不是 ListView
。我打赌你不会后悔的。将 LayoutManager 设置为 HORIZONTAL。按如下操作:
第一个 ADD xml 中的 RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
在片段
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
在onPostExecute()
中:
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(
this,
LinearLayoutManager.HORIZONTAL,
false
);
mRecyclerView.setLayoutManager(mLayoutManager);
Here 是一个简单的 Horizontal RecyclerView 教程。
在这里您可以找到有关如何使用回收器视图水平设置列表视图的教程
https://www.codeproject.com/Articles/1152595/Android-Horizontal-ListView-Tutorial
列表视图不支持线性布局的水平滚动
将此库用于水平 ListView。奇迹般有效。
在此之前,我使用的是 ListView,它工作正常。但我想将 ListView 设置为水平。所以我开始知道 RecycleView 比 listview 更可靠。
我正在尝试使用回收视图并收到类似
的错误 No adapter attached; skipping layout
这意味着问题出在我的适配器中class我不知道我的适配器中需要什么代码class我只是这样试过。
main.java
public class DynamicButon extends Fragment {
public DynamicButon(){};
JSONObject jsonobject;
JSONArray ownerObj;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
ListViewAdapterDynamic mAdapter;
ArrayList<HashMap<String, String>> arraylist;
String uid = "0";
SessionManager session;
private static String url_visitor = "";
JSONParser jParser = new JSONParser();
ArrayList<String> itemwod = new ArrayList<String>();
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_dynamic_buton, container, false);
getActivity().setTitle("Complete Order");
// listview = (ListView) view.findViewById(R.id.lvvisit);
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
uid = user.get(SessionManager.KEY_ID);
new DownloadJSON().execute();
return view;
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// avi.show();
}
@Override
protected Void doInBackground(Void... args) {
try {
arraylist = new ArrayList<HashMap<String, String>>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", uid));
JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);
int success1 = Integer.parseInt(json.getString("success4"));
Log.d("success4", json.toString());
if (success1 == 0) {
Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
}
if (success1 == 1) {
ownerObj = json.getJSONArray("cy");
for (int i = 0; i < ownerObj.length(); i++) {
jsonobject = ownerObj.getJSONObject(i);
item.add(jsonobject.getString("company"));
}
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void args) {
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.mylist);
mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ListViewAdapterDynamic(getActivity(), itemwod);
mRecyclerView.setAdapter(mAdapter);
}
}
}
ListViewAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by sachin on 3/15/2017.
*/
public class ListViewAdapterDynamic extends BaseAdapter {
Context cntx;
View view;
ArrayList<String> itemwod = new ArrayList<String>();
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapterDynamic(Context context,
ArrayList<String> o_parties
) {
// TODO Auto-generated constructor stub
cntx = context;
itemwod = o_parties;
}
@Override
public int getCount() {
return itemwod.size();
}
@Override
public Object getItem(int position) {
return itemwod.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
public View getView(final int position, View convertView, ViewGroup parent) {
Button cancel;
inflater = (LayoutInflater) cntx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(cntx);
convertView = inflater.inflate(R.layout.raw_dynamic, parent,
false);
cancel=(Button)convertView.findViewById(R.id.btn) ;
cancel.setText(itemwod.get(position));
return convertView;
}
}
像这样我尝试但没有在 ListViewAdapterDynamic 中工作我试图实现
RecyclerView.Adapter<MyViewHolder>
然后出现错误,这就是为什么我只是继续扩展 Baseadapter 我在列表视图中所做的。
使用 RecyclerView 设置列表项。
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);
使用回收视图。
在水平方向上使用 Recyclerview 按照这个 link https://developer.android.com/training/material/lists-cards.html
使用 RecyclerView
而不是 ListView
。我打赌你不会后悔的。将 LayoutManager 设置为 HORIZONTAL。按如下操作:
第一个 ADD xml 中的 RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
在片段
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
在onPostExecute()
中:
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(
this,
LinearLayoutManager.HORIZONTAL,
false
);
mRecyclerView.setLayoutManager(mLayoutManager);
Here 是一个简单的 Horizontal RecyclerView 教程。
在这里您可以找到有关如何使用回收器视图水平设置列表视图的教程
https://www.codeproject.com/Articles/1152595/Android-Horizontal-ListView-Tutorial
列表视图不支持线性布局的水平滚动
将此库用于水平 ListView。奇迹般有效。