在此之前,我正在使用ListView,它工作正常。但我想将ListView设置为水平。所以我开始知道RecycleView比listview更可靠。
我正在尝试使用回收视图并收到类似
的错误 No adapter attached; skipping layout
这意味着问题出在我的适配器类中,我不知道我的适配器类需要什么代码我只是这样尝试。
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我所做的listview。
答案 0 :(得分:2)
使用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。
答案 1 :(得分:0)
使用RecyclerView设置列表项目。
答案 2 :(得分:0)
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);
使用recylerview。
答案 3 :(得分:0)
使用具有水平方向的回收视图 点击此链接 https://developer.android.com/training/material/lists-cards.html
答案 4 :(得分:0)
在这里,您可以找到有关如何使用回收站视图水平设置列表视图的教程
https://www.codeproject.com/Articles/1152595/Android-Horizontal-ListView-Tutorial
listview不支持线性布局中的水平滚动
答案 5 :(得分:0)
将此库用于Horizontal ListView。奇迹般有效。