Create separate fragment for search; extract SearchResult to separate file

This commit is contained in:
alex 2018-03-27 18:28:52 +03:00
parent abc796d823
commit 939902053d
8 changed files with 275 additions and 147 deletions

View file

@ -5,31 +5,23 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/ctx_menu_info_view_bg"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/dashboard_map_toolbar"
osmand:contentInsetEnd="0dp"
osmand:contentInsetLeft="4dp"
osmand:contentInsetRight="0dp"
osmand:contentInsetStart="4dp">
android:minHeight="@dimen/dashboard_map_toolbar"
android:theme="?attr/toolbar_theme"
osmand:contentInsetLeft="54dp"
osmand:contentInsetStart="54dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<ImageButton
android:id="@+id/back_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:contentDescription="@string/shared_string_back"
tools:src="@drawable/ic_arrow_back"/>
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/toolbar_text"
android:layout_width="0dp"
@ -64,40 +56,33 @@
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="@+id/search_button"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_height="@dimen/dashboard_map_toolbar"
android:background="?attr/bg_color"
android:gravity="center_vertical">
<EditText
android:id="@+id/search_edit_text"
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:background="@null"
android:ellipsize="end"
android:gravity="center_vertical"
android:hint="@string/search_poi_category_hint"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:textColor="?attr/searchbar_text"
android:textColorHint="?attr/searchbar_text_hint"/>
android:text="@string/search_poi_category_hint"
android:textColor="?attr/searchbar_text_hint"
android:textSize="@dimen/default_list_text_size_large"/>
<ImageButton
android:id="@+id/search_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="48dp"
android:layout_height="48dp"
<ImageView
android:id="@+id/search_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:contentDescription="@string/shared_string_search"
tools:src="@drawable/ic_action_search_dark"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

View file

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/ctx_menu_info_view_bg"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/dashboard_map_toolbar"
android:background="?attr/bg_color"
android:minHeight="@dimen/dashboard_map_toolbar"
android:theme="?attr/toolbar_theme"
app:contentInsetLeft="54dp"
app:contentInsetStart="54dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<EditText
android:id="@+id/search_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/shared_string_search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:textColor="?attr/searchbar_text"
android:textColorHint="?attr/searchbar_text_hint"/>
<ImageButton
android:id="@+id/search_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:contentDescription="@string/shared_string_search"
tools:src="@drawable/ic_action_search_dark"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

View file

@ -3,7 +3,9 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:background="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:layout_width="match_parent"
@ -65,8 +67,4 @@
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="25dp"/>
</LinearLayout>

View file

@ -3,23 +3,15 @@ package net.osmand.plus.wikivoyage;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ImageView;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
import net.osmand.plus.wikivoyage.data.WikivoyageDbHelper.SearchResult;
import java.util.ArrayList;
import java.util.List;
import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment;
public class WikivoyageExploreDialogFragment extends BaseOsmAndDialogFragment {
@ -30,39 +22,23 @@ public class WikivoyageExploreDialogFragment extends BaseOsmAndDialogFragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View mainView = inflater.inflate(R.layout.fragment_wikivoyage_explore_dialog, container);
final EditText searchEt = (EditText) mainView.findViewById(R.id.search_edit_text);
final SearchListAdapter adapter = new SearchListAdapter();
RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(adapter);
ImageButton backBtn = (ImageButton) mainView.findViewById(R.id.back_button);
backBtn.setImageDrawable(getContentIcon(R.drawable.ic_arrow_back));
backBtn.setOnClickListener(new View.OnClickListener() {
Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getContentIcon(R.drawable.ic_arrow_back));
toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
public void onClick(View v) {
dismiss();
}
});
ImageButton searchBtn = (ImageButton) mainView.findViewById(R.id.search_button);
searchBtn.setImageDrawable(getContentIcon(R.drawable.ic_action_search_dark));
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.setItems(getMyApplication().getWikivoyageDbHelper().search((searchEt).getText().toString()));
}
});
((ImageView) mainView.findViewById(R.id.search_icon))
.setImageDrawable(getContentIcon(R.drawable.ic_action_search_dark));
searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
mainView.findViewById(R.id.search_button).setOnClickListener(new View.OnClickListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
adapter.setItems(getMyApplication().getWikivoyageDbHelper().search((searchEt).getText().toString()));
return true;
}
return false;
public void onClick(View v) {
WikivoyageSearchDialogFragment.showInstance(getFragmentManager());
}
});
@ -78,51 +54,4 @@ public class WikivoyageExploreDialogFragment extends BaseOsmAndDialogFragment {
return false;
}
}
private static class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> {
private List<SearchResult> items = new ArrayList<>();
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.wikivoyage_search_list_item, viewGroup, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
SearchResult item = items.get(i);
viewHolder.searchTerm.setText(item.getSearchTerm());
viewHolder.cityId.setText(String.valueOf(item.getCityId()));
viewHolder.articleTitle.setText(item.getArticleTitle());
viewHolder.lang.setText(item.getLang());
}
@Override
public int getItemCount() {
return items.size();
}
public void setItems(List<SearchResult> items) {
this.items = items;
notifyDataSetChanged();
}
static class ViewHolder extends RecyclerView.ViewHolder {
final TextView searchTerm;
final TextView cityId;
final TextView articleTitle;
final TextView lang;
public ViewHolder(View itemView) {
super(itemView);
searchTerm = (TextView) itemView.findViewById(R.id.search_term);
cityId = (TextView) itemView.findViewById(R.id.city_id);
articleTitle = (TextView) itemView.findViewById(R.id.article_title);
lang = (TextView) itemView.findViewById(R.id.lang);
}
}
}
}

View file

@ -0,0 +1,25 @@
package net.osmand.plus.wikivoyage.data;
public class SearchResult {
String searchTerm;
long cityId;
String articleTitle;
String lang;
public String getSearchTerm() {
return searchTerm;
}
public long getCityId() {
return cityId;
}
public String getArticleTitle() {
return articleTitle;
}
public String getLang() {
return lang;
}
}

View file

@ -105,28 +105,4 @@ public class WikivoyageDbHelper {
private static File getDbFile(OsmandApplication app) {
return app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR + DB_NAME);
}
public static class SearchResult {
private String searchTerm;
private long cityId;
private String articleTitle;
private String lang;
public String getSearchTerm() {
return searchTerm;
}
public long getCityId() {
return cityId;
}
public String getArticleTitle() {
return articleTitle;
}
public String getLang() {
return lang;
}
}
}

View file

@ -0,0 +1,66 @@
package net.osmand.plus.wikivoyage.search;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.data.SearchResult;
import java.util.ArrayList;
import java.util.List;
public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecyclerViewAdapter.ViewHolder> {
private List<SearchResult> items = new ArrayList<>();
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.wikivoyage_search_list_item, viewGroup, false);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
SearchResult item = items.get(i);
viewHolder.searchTerm.setText(item.getSearchTerm());
viewHolder.cityId.setText(String.valueOf(item.getCityId()));
viewHolder.articleTitle.setText(item.getArticleTitle());
viewHolder.lang.setText(item.getLang());
}
@Override
public int getItemCount() {
return items.size();
}
public void setItems(List<SearchResult> items) {
this.items = items;
notifyDataSetChanged();
}
static class ViewHolder extends RecyclerView.ViewHolder {
final TextView searchTerm;
final TextView cityId;
final TextView articleTitle;
final TextView lang;
public ViewHolder(View itemView) {
super(itemView);
searchTerm = (TextView) itemView.findViewById(R.id.search_term);
cityId = (TextView) itemView.findViewById(R.id.city_id);
articleTitle = (TextView) itemView.findViewById(R.id.article_title);
lang = (TextView) itemView.findViewById(R.id.lang);
}
}
}

View file

@ -0,0 +1,86 @@
package net.osmand.plus.wikivoyage.search;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment {
public static final String TAG = "WikivoyageSearchDialogFragment";
private SearchRecyclerViewAdapter adapter;
private EditText searchEt;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View mainView = inflater.inflate(R.layout.fragment_wikivoyage_search_dialog, container);
Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getContentIcon(R.drawable.ic_arrow_back));
toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
searchEt = (EditText) toolbar.findViewById(R.id.search_edit_text);
searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
runSearch();
return true;
}
return false;
}
});
ImageButton searchBtn = (ImageButton) mainView.findViewById(R.id.search_button);
searchBtn.setImageDrawable(getContentIcon(R.drawable.ic_action_search_dark));
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
runSearch();
}
});
adapter = new SearchRecyclerViewAdapter();
RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(adapter);
return mainView;
}
private void runSearch() {
adapter.setItems(getMyApplication().getWikivoyageDbHelper().search((searchEt).getText().toString()));
}
public static boolean showInstance(FragmentManager fm) {
try {
WikivoyageSearchDialogFragment fragment = new WikivoyageSearchDialogFragment();
fragment.show(fm, TAG);
return true;
} catch (RuntimeException e) {
return false;
}
}
}