请帮忙..
我有一个项目,使用recylerview
在searchview
中显示政策数据。
现在,数据已完整显示..但我计划添加searchview
..当我添加代码以显示searchview
时,searchview
无法显示...请帮助我检查或更正我的代码:(
抱歉我的英语不好..
这是我的代码:
PolicyActivity:
public class PolicyActivity extends AppCompatActivity {
SearchView searchView;
List<PolicyNo> policyNo;
private static final String TAG = PolicyActivity.class.getSimpleName();
private String policyno;
private RecyclerView recyclerView;
PolicyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_policy);
recyclerView = findViewById(R.id.policy_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiEndPoint apiEndPoint =
ApiClient.getClient(this).create(ApiEndPoint.class);
Call<PolicyStatus> call = apiEndPoint.getPolicyNo(policyno);
call.enqueue(new Callback<PolicyStatus>() {
@Override
public void onResponse(Call<PolicyStatus> call, Response<PolicyStatus> response) {
List<PolicyNo> policyNo = response.body().getPolicyNo();
Log.d(TAG, "Number Policy Received: " + policyNo.size());
recyclerView.setAdapter(new PolicyAdapter(policyNo, R.layout.list_item_policy, getApplicationContext()));
}
@Override
public void onFailure(Call<PolicyStatus> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchfile, menu);
final MenuItem myActionMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) myActionMenuItem.getActionView();
changeSearchViewTextColor(searchView);
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.White));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (!searchView.isIconified()) {
searchView.setIconified(true);
}
myActionMenuItem.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
final List<PolicyNo> filtermodelist = filter(policyNo, newText);
adapter.setFilter(filtermodelist);
return true;
}
});
return true;
}
private List<PolicyNo> filter(List<PolicyNo> nopolis, String query) {
query = query.toLowerCase();
final List<PolicyNo> filteredModeList = new ArrayList<>();
for (PolicyNo model : nopolis) {
final String text = model.getPolicyNo().toLowerCase();
if (text.startsWith(query)) {
filteredModeList.add(model);
}
}
return filteredModeList;
}
private void changeSearchViewTextColor(View view) {
if (view != null) {
if (view instanceof TextView) {
((TextView) view).setTextColor(Color.WHITE);
return;
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
changeSearchViewTextColor(viewGroup.getChildAt(i));
}
}
}
}
}
&#13;
PolicyAdapter:
public class PolicyAdapter extends RecyclerView.Adapter<PolicyAdapter.PolicyViewHolder> {
private List<PolicyNo> policyNo;
private int rowLayout;
private Context context;
public static class PolicyViewHolder extends RecyclerView.ViewHolder{
LinearLayout policyLayout;
TextView nomorpolis;
public PolicyViewHolder (View v){
super(v);
policyLayout = v.findViewById(R.id.policy_layout);
nomorpolis = v.findViewById(R.id.nomorpolis);
}
}
public PolicyAdapter (List<PolicyNo>policyNo, int rowLayout, Context context){
this.policyNo = policyNo;
this.context = context;
this.rowLayout = rowLayout;
}
@Override
public PolicyAdapter.PolicyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view= LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new PolicyViewHolder(view);
}
@Override
public void onBindViewHolder (PolicyViewHolder holder, final int position){
holder.nomorpolis.setText(policyNo.get(position).getPolicyNo());
}
@Override
public int getItemCount() {return policyNo.size();}
public void setFilter(List<PolicyNo> policyNoList)
{
policyNo = new ArrayList<>();
policyNo.addAll(policyNoList);
notifyDataSetChanged();
}
}
&#13;
菜单目录中的searchfile.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_action_search"
android:orderInCategory="300"
android:title="Search..."
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
&#13;
ActivityPolicy.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_policy"
android:background="@drawable/back">
<android.support.v7.widget.RecyclerView
android:id="@+id/policy_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
&#13;
列表项policy.xml =
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/policy_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:background="@color/Orange">
<TextView
android:id="@+id/nopolis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Policy :"/>
<TextView
android:id="@+id/nomorpolis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/nomorpolis"
android:textSize="20sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
&#13;
清单:
<application
android:allowBackup="true"
android:icon="@drawable/logoiai"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:screenOrientation="portrait" />
<activity android:name=".HomeActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".Splashscreen"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@style/Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PolicyActivity"
android:screenOrientation="portrait" />
</application>
</manifest>
&#13;
样式:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
&#13;
现在捕获条件: enter image description here