如何创建返回搜索结果主页活动(Activity_Main)的搜索对话框?

时间:2019-04-28 14:05:29

标签: java android

我是刚接触Android Studio的人,我想要在家庭活动(Activity_Main)顶部创建搜索对话框 请参阅图片LinK 1 => https://drive.google.com/drive/folders/1y4A7sWXBWIDM7Mf4BoDp7ak2e3cgb4Qu并在使用时单击搜索按钮或搜索项目建议,将数据返回到家庭活动(Activity_Main)并显示在mmTextViewItemSearched上选择的文本用户类型或项目 请参阅图片链接2 =>“ https://drive.google.com/drive/folders/1y4A7sWXBWIDM7Mf4BoDp7ak2e3cgb4Qu” 塞住。

这是我所拥有的,搜索视图上的用户类型在列表视图中显示关于家庭活动(Activity_Main)的数据建议 请参阅图片链接 3 =>“ https://drive.google.com/drive/folders/1y4A7sWXBWIDM7Mf4BoDp7ak2e3cgb4Qu

下面是我的代码文件包含的内容。

1)activity_main.XML

2)Activity_Main.JAVA

3)User_Detail.JAVA

4)Adapter.JAVA

1) activity_main.XML

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:orientation="vertical"
tools:context=".Activity_Main">

<android.support.design.widget.AppBarLayout
    android:id="@+id/xxAppBarLayout"
    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="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:collapseIcon="@drawable/icon_search_collapse"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<TextView
    android:id="@+id/mmTextViewHome"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/xxAppBarLayout"
    android:layout_centerInParent="true"
    android:layout_marginTop="16dp"
    android:gravity="left"
    android:text="Home"
    android:textAllCaps="false"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
    android:textSize="16sp"
    android:typeface="serif"
    android:layout_marginHorizontal="32dp"
    />

<TextView
    android:id="@+id/mmTextViewItemSearched"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mmTextViewHome"
    android:layout_centerInParent="true"
    android:layout_marginTop="8dp"
    android:gravity="left"
    android:text="TextView Display Item Searched By User"
    android:textAllCaps="false"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
    android:textSize="16sp"
    android:typeface="serif"
    android:layout_marginHorizontal="32dp"
    android:layout_marginBottom="8dp"
    />
<ProgressBar
    android:id="@+id/mmProgressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentBottom="true"
    />

<android.support.v7.widget.RecyclerView
    android:id="@+id/mmRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/mmTextViewItemSearched"
    android:layout_marginHorizontal="16dp" />

2) Activity_Main.JAVA

public class Activity_Main extends AppCompatActivity {
private Toolbar mToolbar;
private TextView mmTextViewHTML;
private int mmLatestRowNumber = 0;
private String mmSearchxKey = "";
private boolean mmSearching_TF = false;
private RecyclerView mmRecyclerView;
private GridLayoutManager mmGridLayoutManager;
private List<User_Detail> mmUser_Details;
private List<User_Detail> mmDataBase_User_Details;
private Adapter mmAdapter;
private ProgressBar mmProgressBar;



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

    //  initialisation of variable
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    // DataBase which store UserDetail OnLine Or OffLine
    F_DataBaseUser_Detai();

    mmUser_Details = new ArrayList<>();
    mmProgressBar = findViewById(R.id.mmProgressBar);
    mmRecyclerView = findViewById(R.id.mmRecyclerView);
    mmGridLayoutManager = new GridLayoutManager(this, 1);
    mmRecyclerView.setLayoutManager(mmGridLayoutManager);
    mmRecyclerView.setHasFixedSize(true);

    mmAdapter = new Adapter(mmUser_Details, this);
    mmRecyclerView.setAdapter(mmAdapter);


    mmRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);


            if (mmGridLayoutManager.findLastCompletelyVisibleItemPosition() == mmUser_Details.size() - 1 && mmLatestRowNumber < mmUser_Details.size()) {

                mmLatestRowNumber = mmUser_Details.size();
                mmProgressBar.setVisibility(View.VISIBLE);
                F_FetchUser_Detai(mmLatestRowNumber,mmSearchxKey);
            }

        }
    });

    F_FetchUser_Detai(mmLatestRowNumber,mmSearchxKey);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_search, menu);

    MenuItem xxSearchMenuItem = menu.findItem(R.id.action_search);
    SearchView xxSearchView = (SearchView) xxSearchMenuItem.getActionView();

    MenuItemCompat.setOnActionExpandListener(xxSearchMenuItem, new MenuItemCompat.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {

            //  Start Searching mmListShopping OR mmListSeeking
            //  NOT mmListHome => Have Fiw thing to show them as Intro

            //Log.d("TAG", "onMenuItemAction_Expand");
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {

            //  Close Searching mmListShopping OR mmListSeeking
            //  NOT mmListHome => Have Fiw thing to show them as Intro

            if(mmSearching_TF) {

                mmSearching_TF = !mmSearching_TF;
                mmLatestRowNumber = 0;
                mmSearchxKey = "";
                mmUser_Details.clear();
                mmProgressBar.setVisibility(View.VISIBLE);
                F_FetchUser_Detai(mmLatestRowNumber,mmSearchxKey);
            }

            return true;
        }
    });


    xxSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String xxString){

            if(xxString.length() >= 3) {

                mmSearching_TF = !mmSearching_TF;
                mmLatestRowNumber = 0;
                mmSearchxKey = xxString;
                mmUser_Details.clear();
                mmProgressBar.setVisibility(View.VISIBLE);
                F_FetchUser_Detai(mmLatestRowNumber,mmSearchxKey);
            }
            return false;
        }
    });


    return true;
}

private void F_FetchUser_Detai(int xxUser_ID, String xxUser_Name){

    int xxLimt = xxUser_ID + 15;
    Toast.makeText(this, "Load Data Online or OffLine DataBase", Toast.LENGTH_SHORT).show();


        for (int xxIndex = xxUser_ID; xxIndex <= mmDataBase_User_Details.size()-1; xxIndex++) {

            // To Avoid IndexOutOfBoundsException ERROR mmDataBase_User_Details.size()-1
            // name match condition.
            // here we are looking for name

            if (mmDataBase_User_Details.get(xxIndex).getUser_Name().toLowerCase().contains(xxUser_Name.toLowerCase())) {
                mmUser_Details.add(mmDataBase_User_Details.get(xxIndex));

                Log.d("TAG", "xxUser_ID = : " + xxUser_ID);
                if (xxIndex >= xxLimt) {
                    mmProgressBar.setVisibility(View.GONE);
                    mmAdapter.notifyDataSetChanged();
                    break;
                }
            }

        }

    mmProgressBar.setVisibility(View.GONE);
    mmAdapter.notifyDataSetChanged();
}

private void F_DataBaseUser_Detai() {
    mmDataBase_User_Details = new ArrayList<>();
    //  A User
    mmDataBase_User_Details.add(F_New_User_Detail(1,"aaaa user 1"));
    mmDataBase_User_Details.add(F_New_User_Detail(2,"aaaa user 2"));
    mmDataBase_User_Details.add(F_New_User_Detail(3,"aaaa user 3"));
    mmDataBase_User_Details.add(F_New_User_Detail(4,"aaaa user 4"));
    mmDataBase_User_Details.add(F_New_User_Detail(5,"aaaa user 5"));
    mmDataBase_User_Details.add(F_New_User_Detail(6,"aaaa user 6"));
    mmDataBase_User_Details.add(F_New_User_Detail(7,"aaaa user 7"));
    mmDataBase_User_Details.add(F_New_User_Detail(8,"aaaa user 8"));
    mmDataBase_User_Details.add(F_New_User_Detail(9,"aaaa user 9"));
    mmDataBase_User_Details.add(F_New_User_Detail(10,"aaaa user 10"));

    //  B User
    mmDataBase_User_Details.add(F_New_User_Detail(11,"bbbb user 1"));
    mmDataBase_User_Details.add(F_New_User_Detail(12,"bbbb user 2"));
    mmDataBase_User_Details.add(F_New_User_Detail(13,"bbbb user 3"));
    mmDataBase_User_Details.add(F_New_User_Detail(14,"bbbb user 4"));
    mmDataBase_User_Details.add(F_New_User_Detail(15,"bbbb user 5"));
    mmDataBase_User_Details.add(F_New_User_Detail(16,"bbbb user 6"));
    mmDataBase_User_Details.add(F_New_User_Detail(17,"bbbb user 7"));
    mmDataBase_User_Details.add(F_New_User_Detail(18,"bbbb user 8"));
    mmDataBase_User_Details.add(F_New_User_Detail(19,"bbbb user 9"));
    mmDataBase_User_Details.add(F_New_User_Detail(20,"bbbb user 10"));

    //  C User
    mmDataBase_User_Details.add(F_New_User_Detail(21,"cccc user 1"));
    mmDataBase_User_Details.add(F_New_User_Detail(22,"cccc user 2"));
    mmDataBase_User_Details.add(F_New_User_Detail(23,"cccc user 3"));
    mmDataBase_User_Details.add(F_New_User_Detail(24,"cccc user 4"));
    mmDataBase_User_Details.add(F_New_User_Detail(25,"cccc user 5"));
    mmDataBase_User_Details.add(F_New_User_Detail(26,"cccc user 6"));
    mmDataBase_User_Details.add(F_New_User_Detail(27,"cccc user 7"));
    mmDataBase_User_Details.add(F_New_User_Detail(28,"cccc user 8"));
    mmDataBase_User_Details.add(F_New_User_Detail(29,"cccc user 9"));
    mmDataBase_User_Details.add(F_New_User_Detail(30,"cccc user 10"));

    //  D User
    mmDataBase_User_Details.add(F_New_User_Detail(31,"dddd user 1"));
    mmDataBase_User_Details.add(F_New_User_Detail(32,"dddd user 2"));
    mmDataBase_User_Details.add(F_New_User_Detail(33,"dddd user 3"));
    mmDataBase_User_Details.add(F_New_User_Detail(34,"dddd user 4"));
    mmDataBase_User_Details.add(F_New_User_Detail(35,"dddd user 5"));
    mmDataBase_User_Details.add(F_New_User_Detail(36,"dddd user 6"));
    mmDataBase_User_Details.add(F_New_User_Detail(37,"dddd user 7"));
    mmDataBase_User_Details.add(F_New_User_Detail(38,"dddd user 8"));
    mmDataBase_User_Details.add(F_New_User_Detail(39,"dddd user 9"));
    mmDataBase_User_Details.add(F_New_User_Detail(40,"dddd user 10"));

    //  E User
    mmDataBase_User_Details.add(F_New_User_Detail(41,"eeee user 1"));
    mmDataBase_User_Details.add(F_New_User_Detail(42,"eeee user 2"));
    mmDataBase_User_Details.add(F_New_User_Detail(43,"eeee user 3"));
    mmDataBase_User_Details.add(F_New_User_Detail(44,"eeee user 4"));
    mmDataBase_User_Details.add(F_New_User_Detail(45,"eeee user 5"));
    mmDataBase_User_Details.add(F_New_User_Detail(46,"eeee user 6"));
    mmDataBase_User_Details.add(F_New_User_Detail(47,"eeee user 7"));
    mmDataBase_User_Details.add(F_New_User_Detail(48,"eeee user 8"));
    mmDataBase_User_Details.add(F_New_User_Detail(49,"eeee user 9"));
    mmDataBase_User_Details.add(F_New_User_Detail(50,"eeee user 10"));

    //  F User
    mmDataBase_User_Details.add(F_New_User_Detail(51,"ffff user 1"));
    mmDataBase_User_Details.add(F_New_User_Detail(52,"ffff user 2"));
    mmDataBase_User_Details.add(F_New_User_Detail(53,"ffff user 3"));
    mmDataBase_User_Details.add(F_New_User_Detail(54,"ffff user 4"));
    mmDataBase_User_Details.add(F_New_User_Detail(55,"ffff user 5"));
    mmDataBase_User_Details.add(F_New_User_Detail(56,"ffff user 6"));
    mmDataBase_User_Details.add(F_New_User_Detail(57,"ffff user 7"));
    mmDataBase_User_Details.add(F_New_User_Detail(58,"ffff user 8"));
    mmDataBase_User_Details.add(F_New_User_Detail(59,"ffff user 9"));
    mmDataBase_User_Details.add(F_New_User_Detail(60,"ffff user 10"));
}

private User_Detail F_New_User_Detail(int xxUser_ID,String xxUser_Name){

    User_Detail xxUser_Detail = new User_Detail();
    xxUser_Detail.setUser_ID(xxUser_ID);
    xxUser_Detail.setUser_Name(xxUser_Name);
    return xxUser_Detail;
}
}

3) User_Detail.JAVA

public class Adapter extends RecyclerView.Adapter<Adapter.CcViewHolder> {
//  Initialization of object gone use in Adapter Class
private List<User_Detail> mmUser_Details;
private Context mmContext;

public Adapter(List<User_Detail> mmUser_Details, Context mmContext) {
    this.mmUser_Details = mmUser_Details;
    this.mmContext = mmContext;
}

@NonNull
@Override
public CcViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

    View search_view_suggestion_row = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_view_suggestion_row,parent,false);
    return new CcViewHolder(search_view_suggestion_row);
}

@Override
public void onBindViewHolder(@NonNull CcViewHolder xxCcViewHolder, int xxPosition) {

    String xxUserID = " User ID : "+ mmUser_Details.get(xxPosition).getUser_ID();
    String xxUserName = " User Name : " + mmUser_Details.get(xxPosition).getUser_Name();

    xxCcViewHolder.mmKey.setText(Html.fromHtml(getHTMLString(xxUserID,"blue",xxUserName.toLowerCase(),"red")));
}

@Override
public int getItemCount() {
    return mmUser_Details.size();
}

public static class CcViewHolder extends RecyclerView.ViewHolder{

    //  Initialization of object gone use in CcViewHolder Class
    TextView mmKey;

    public CcViewHolder(@NonNull View itemView) {
        super(itemView);
        mmKey = itemView.findViewById(R.id.mmKey);
    }
}

private String getHTMLString(String xxUserID, String xxColorUserID, String xxUserName, String xxColorUserName) {
    //  large , medium, small
    String xxNewHTMLString =    "<u><small><font color=\""+ xxColorUserID +"\">" + xxUserID + "</font></small></u>" +
            "<br/> <small> <font color=\"" + xxColorUserName + "\">" + xxUserName + "</font> </small>";
    return xxNewHTMLString;
}
}

4) Adapter.JAVA

package com.robertpro.diliappv1;


public class User_Detail {

 private  int user_ID;
 private  String user_Name;

public int getUser_ID() {
    return user_ID;
}

public void setUser_ID(int user_ID) {
    this.user_ID = user_ID;
}

public String getUser_Name() {
    return user_Name;
}

public void setUser_Name(String user_Name) {
    this.user_Name = user_Name;
}

}

0 个答案:

没有答案