在我的应用程序中,我有一个从json文件填充的图像的回收视图。在我的工具栏菜单中,我添加了一个选项,根据该选择过滤图像。我的json中的每个项目都标有一个选择类别(包括示例)。我试图找出如何根据用户选择过滤此Recyclerview列表。我搜索和搜索,但似乎无法找到解决方案。如果有人可以帮助解决sudocode或一个例子,我会非常感激。以下是我的课程。
json示例:
{ "image_url": "http://www.nm-studios.com/trek_lines.png",
"title":"Trek1",
"package_url":"http://www.nm-studios.com/trek_lines.png",
"id":lines},
{ "image_url": "http://www.nm-studios.com/stuffs/trek_lines2.png",
"title":"Trek2",
"premium":"PRO ONLY",
"package_url":"http://www.nm-studios.com/trek_lines2.png",
"id":lines},
{ "image_url": "http://www.nm-studios.com/wall_start.png",
"title":"GreyShadowed",
"premium":"PRO ONLY",
"package_url":"http://www.nm-studios.com/wall_start.png",
"id":lines},
用于显示列表的片段:
public class WallpapersFragment extends Fragment {
private static final String TAG = "RecyclerViewExample";
private List<FeedWallpapers> feedItemList = new ArrayList<FeedWallpapers>();
//added static might remove
private static RecyclerView mRecyclerView;
private static MyRecyclerAdapterWallpapers adapter;
public String filter_list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.fragment_wall_recycler, container, false);
setHasOptionsMenu(true);
/* Allow activity to show indeterminate progress-bar */
//getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
/* Initialize recycler view */
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_wall_recycler);
mRecyclerView.setHasFixedSize(true);
//mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/*Downloading data from below url*/
final String url = "http://www.nm-studios.com/Json/wallpaper_list.json";
new AsyncHttpTask().execute(url);
return view;
}
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
/* @Override
protected void onPreExecute() {
setProgressBarIndeterminateVisibility(true);
}*/
@Override
protected Integer doInBackground(String... params) {
InputStream inputStream = null;
Integer result = 0;
HttpURLConnection urlConnection = null;
try {
/* forming th java.net.URL object */
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
/* for Get request */
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
/* 200 represents HTTP OK */
if (statusCode == 200) {
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
response.append(line);
}
parseResult(response.toString());
result = 1; // Successful
}else{
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}
@Override
protected void onPostExecute(Integer result) {
// setProgressBarIndeterminateVisibility(false);
/* Download complete. Lets update UI */
if (result == 1) {
adapter = new MyRecyclerAdapterWallpapers(getActivity(), feedItemList);
mRecyclerView.setAdapter(adapter);
} else {
Log.e(TAG, "Failed to fetch data!");
}
}
}
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray products = response.optJSONArray("products");
/*Initialize array if null*/
if (null == feedItemList) {
feedItemList = new ArrayList<FeedWallpapers>();
}
for (int i = 0; i < products.length(); i++) {
JSONObject product = products.optJSONObject(i);
FeedWallpapers item = new FeedWallpapers();
item.setTitle(product.optString("title"));
item.setImage_url(product.optString("image_url"));
item.setPackage_url(product.optString("package_url"));
item.setPremium_wall(product.optString("premium"));
item.setIds(product.optString("ids"));
feedItemList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.wall_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.all:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
filter_list = "";
return true;
case R.id.text:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
filter_list = "android_text";
return true;
case R.id.lines:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
filter_list = "lines";
return true;
case R.id.windows:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
filter_list = "windows";
return true;
case R.id.material:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
filter_list = "material";
return true;
case R.id.rivalry:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
filter_list = "rivalry";
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我的持有人:
public class FeedWallpapersHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView thumbnail;
public TextView titles;
public Context context;
public TextView wall_url;
public int wallPosition;
public TextView premiumTag;
public FeedWallpapersHolder(View view) {
super(view);
view.setOnClickListener(this);
this.thumbnail = (ImageView) view.findViewById(R.id.wall_image);
this.titles = (TextView) view.findViewById(R.id.wall_title);
this.wall_url = (TextView) view.findViewById(R.id.wall_download_url);
this.premiumTag = (TextView) view.findViewById(R.id.premium_tag_walls);
context = view.getContext();
}
@Override
public void onClick(View view) {
wallPosition = getPosition();
//Toast.makeText(view.getContext(), "I'm clicked!! " + wallPosition, Toast.LENGTH_SHORT).show();
//doing some stuff here maybe
final Intent wall_intent = new Intent(view.getContext(), FullScreenActivity.class);
final String WallUrl = ((TextView) view.findViewById(R.id.wall_download_url)).getText().toString();
final String Premium = ((TextView) view.findViewById(R.id.premium_tag_walls)).getText().toString();
final String fn_wall = ((TextView) view.findViewById(R.id.wall_title)).getText().toString() +".png";
wall_intent.putExtra("FullImageUrl", WallUrl);
wall_intent.putExtra("FnWallFinal", fn_wall);
Log.i("premium Check", Premium);
//this starts my premium check
Log.i("launch download dialog", "launching");
Log.i("url", WallUrl);
context.startActivity(wall_intent);
}
}
我的适配器:
public class MyRecyclerAdapterWallpapers extends RecyclerView.Adapter<FeedWallpapersHolder> {
private List<FeedWallpapers> feedItemList;
private Context mContext;
public MyRecyclerAdapterWallpapers(Context context, List<FeedWallpapers> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public FeedWallpapersHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.wall_card_layout, null);
FeedWallpapersHolder mh = new FeedWallpapersHolder(v);
return mh;
}
@Override
public void onBindViewHolder(FeedWallpapersHolder feedListRowHolder, int i) {
FeedWallpapers feedItem = feedItemList.get(i);
int n = feedListRowHolder.thumbnail.getWidth();
Point Size = new Point();
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getSize(Size);
int scrWidth = Size.x;
String nstring = Integer.toString(n);
String nwidth = Integer.toString(scrWidth);
Log.i("width", nwidth);
Picasso.with(mContext).load(feedItem.getImage_url())
.error(R.drawable.placeholder)
//.placeholder(R.drawable.placeholder)
.transform(new FitTransformations(scrWidth,false))
//.fit().centerCrop()
.into(feedListRowHolder.thumbnail);
Log.i("the string", feedItem.getImage_url());
feedListRowHolder.titles.setText(Html.fromHtml(feedItem.getTitle()));
feedListRowHolder.premiumTag.setText(Html.fromHtml(feedItem.getPremium_wall()));
feedListRowHolder.wall_url.setText((Html.fromHtml(feedItem.getPackage_url())));
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
}
答案 0 :(得分:2)
没有足够的时间来分析您的代码,所以我不确定您已经完成了什么,这就是我要做的。 首先从那些json对象中创建对象并将它们放在List中。
创建一个方法,获取这些对象的列表,并按照您想要的方式对它们进行排序(或删除您不想要的项目)。
private List<Object> filterMyList(List<Object> list, String category) {
//...
for (Object obj : list) {
// if (!obj.getCategory().equals(category)) -> remove from list.
}
//...
return editedList;
}
在适配器中创建一个类似
的方法public void updateRecyclerData(List<Object> feedItemList) {
this.feedItemList = feedItemList;
notifyDatasetChanged(); //This will update recyclerview
}
最终产品应如下所示:
adapter.updateRecyclerData(filterMyList(myList, "material"));
显然你想让这些类别成为常量。