显示RecyclerView仅在提交搜索后。

huangapple go评论88阅读模式
英文:

Show Recyclerview only after submitting search

问题

I understand your request. Here's the translated code part without additional content:

我想要在启动MainActivity时使其为空只有在提交搜索后才应显示recyclerview的结果因此它永远不会显示整个recyclerview列表)。到目前为止应用程序可以正常工作但MainActivity启动时显示了推送到recyclerview中的所有数据

如果列表很大这将影响性能因此我需要一种方法来使其为空或填充但不显示它),并仅显示具有搜索结果的recyclerview或者创建一个在MainActivity之前启动的新活动该活动仅包含SearchView然后在MainActivity上显示结果

这是我的当前代码

MainActivity.class不包括导入

public class MainActivity extends AppCompatActivity{

private AdapterListe adapter;
private RESTTask asyncTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RecyclerView recyclerView = findViewById(R.id.lista);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new AdapterListe(this);
    recyclerView.setAdapter(adapter);

    asyncTask = new RESTTask();
    asyncTask.execute(getString(R.string.REST_URL));
}

private class RESTTask extends AsyncTask<String, Void, List<Ontologija>> {
    protected List<Ontologija> doInBackground(String... adresa) {
        // 省略异步任务的代码部分
    }

    protected void onPostExecute(List<Ontologija> podaci) {
        adapter.setPodaci(podaci);
        adapter.notifyDataSetChanged();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // 省略菜单创建的代码部分
}

}

Adapter:

public class AdapterListe extends RecyclerView.Adapter<AdapterListe.Red> implements Filterable {

private List<Ontologija> podaci;
private List<Ontologija> podaciTemp;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// 构造函数接收上下文作为参数
public AdapterListe(Context context) {
    this.mInflater = LayoutInflater.from(context);
    podaci = new ArrayList<>();
}

// 省略 onCreateViewHolder 和 onBindViewHolder 方法的代码部分

// 获取总项数
@Override
public int getItemCount() {
    return podaci == null ? 0 : podaci.size();
}

// 省略其他方法的代码部分
}

请注意,这只是代码的部分翻译。如果您需要任何其他帮助或有其他问题,请随时提出。

英文:

I want the MainActivity to be empty when I start it and only after submitting the search should the recyclerview show the results (so it never shows the whole recyclerview list). As of now, the app works but MainActivity starts with all the data thats pushed into the recyclerview.

If the list was huge it would affect performance, so I either need a way to start it empty (or fill it but not show it) and show recyclerview with just the search results, or make a new activity that would start before MainActivity which would only have the Searchview and then the results on MainActivity.

This is my current code:

MainActivity.class (excluding imports)

public class MainActivity extends AppCompatActivity{
private AdapterListe adapter;
private RESTTask asyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.lista);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new AdapterListe(this);
//adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
asyncTask = new RESTTask();
asyncTask.execute( getString(R.string.REST_URL));
}
private class RESTTask extends AsyncTask&lt;String, Void, List&lt;Ontologija&gt;&gt; {
protected List&lt;Ontologija&gt; doInBackground(String... adresa) {
String stringUrl = adresa[0];
List&lt;Ontologija&gt; vrati = null;
try {
URL myUrl = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)
myUrl.openConnection();
connection.setRequestMethod(&quot;GET&quot;);
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
connection.connect();
InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
Type listType = new TypeToken&lt;ArrayList&lt;Ontologija&gt;&gt;() {
}.getType();
vrati = new Gson().fromJson(reader, listType);
reader.close();
streamReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return vrati;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(List&lt;Ontologija&gt; podaci) {
adapter.setPodaci(podaci);
adapter.notifyDataSetChanged();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.search_icon);
SearchView searchView = (SearchView) item.getActionView();
searchView.setQueryHint(&quot;Pretraži...&quot;);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}

}

Adapter

public class AdapterListe extends RecyclerView.Adapter&lt;AdapterListe.Red&gt; implements Filterable {
private List&lt;Ontologija&gt; podaci;
private List&lt;Ontologija&gt; podaciTemp;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// Podatke proslijedimo kroz konstruktor
public AdapterListe(Context context) {
this.mInflater = LayoutInflater.from(context);
podaci = new ArrayList&lt;&gt;();
}
// napuni predložak red (datoteka red_liste.xml)
@Override
public Red onCreateViewHolder(ViewGroup roditelj, int viewType) {
podaciTemp = new ArrayList&lt;&gt;(podaci);
View view = mInflater.inflate(R.layout.red_liste, roditelj, false);
return new Red(view);
}
// Veže podatke za svaki red
@Override
public void onBindViewHolder(Red red, int position) {
Ontologija o = podaci.get(position);
red.naziv.setText(o.getNaziv());
red.tip.setText(o.getTip());
red.opis.setText(o.getOpis());
red.anotacija.setText(o.getAnotacija());
}
// Ukupan broj redova (mora biti implementirano)
@Override
public int getItemCount() {
return podaci==null ? 0 : podaci.size();
}
// Pohranjuje i reciklira pogled kako se prolazi kroz listu
public class Red extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView naziv;
private TextView tip;
private TextView opis;
private TextView anotacija;
Red(View itemView) {
super(itemView);
naziv = itemView.findViewById(R.id.naziv);
tip = itemView.findViewById(R.id.tip);
opis = itemView.findViewById(R.id.opis);
anotacija = itemView.findViewById(R.id.anotacija);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// klikom na listu dobijemo samo poziciju koju stavku liste smo odabrali.
// Ova metoda pomaže da na osnovu pozicije dobijemo cijeli objekt u toj stavci
public Ontologija getItem(int id) {
return podaci.get(id);
}
public void setPodaci(List&lt;Ontologija&gt; itemList) {
this.podaci = itemList;
}
// dopusti hvatanje odabira (klik/dotakni)
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// potrebno kako bi mogli hvatati klikove/dodire
public interface ItemClickListener {
void onItemClick(View view, int position);
}
@Override
public Filter getFilter() {
return new Filter() {
//background
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List&lt;Ontologija&gt; filteredList = new ArrayList&lt;&gt;();
if (constraint.toString().isEmpty()) {
filteredList.addAll(podaciTemp);
} else {
if(constraint.toString().length() &gt;= 3) {
for (Ontologija element : podaciTemp) {
if (element.getNaziv().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(element);
}
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
//ui
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
podaci.clear();
podaci.addAll((Collection&lt;? extends Ontologija&gt;) filterResults.values);
notifyDataSetChanged();
}
};
}

}

Either code, some reference videos/posts or basically any type of help would be appreciated, but so far I've only seen basic filter searchview tutorials of whole recyclerview, nothing of help for my specific situation.

答案1

得分: 1

Here is the translated code:

if you dont want show just remove notifyDataSetChanged

protected void onPostExecute(List<Ontology> data) {
    adapter.setData(data);
    //adapter.notifyDataSetChanged();
}
Edit:

if (!query.isEmpty()) {
    adapter.getFilter().filter(query);
}
or use `searchView.clearFocus();`
英文:

if you dont want show just remove notifyDataSetChanged

protected void onPostExecute(List&lt;Ontologija&gt; podaci) {
adapter.setPodaci(podaci);
//adapter.notifyDataSetChanged();
}

Edit :

if (!query.isEmpty()) {
adapter.getFilter().filter(query);
}

or use searchView.clearFocus();

huangapple
  • 本文由 发表于 2020年8月13日 23:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63398461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定