Używam układu karty z dwoma fragmentami, które mają widok Recycler, a także dodałem widok wyszukiwania w układzie karty, ale tutaj mam problem, chcę, aby widok wyszukiwania działał na obu fragmentach, które dodałem jako kartę, ale widok wyszukiwania jest w innym działaniu (Pasek narzędzi) i oba fragmenty są różne

Aktywność z zakładkami (aktywność LanguageChooser)

 package com.piyushjaiswal.lyricswala;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.SearchView;
    import androidx.appcompat.widget.Toolbar;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentPagerAdapter;
    import androidx.viewpager.widget.ViewPager;

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;

    import com.google.android.material.tabs.TabLayout;

    public class LanguageChooser extends AppCompatActivity {
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_language_chooser);
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            SectionPagerAdapter pagerAdapter = new SectionPagerAdapter(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            ViewPager pager = findViewById(R.id.pager);
            pager.setAdapter(pagerAdapter);
            TabLayout tabLayout = findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(pager);

        }
        private class SectionPagerAdapter extends FragmentPagerAdapter {

            public SectionPagerAdapter(@NonNull FragmentManager fm, int behaviour) {
                super(fm,behaviour);
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                switch (position){
                    case 0:
                        return new Hindi();
                    case 1:
                        return new Punjabi();

                }
                return null;
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position)
                {
                    case 0: return getResources().getText(R.string.Hindi);
                    case 1:return getResources().getText(R.string.Punjabi);
                 }
                return  null;
            }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            getMenuInflater().inflate(R.menu.toolbar,menu);

            return super.onCreateOptionsMenu(menu);
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch(item.getItemId())
            {
                 default:
                    return super.onOptionsItemSelected(item);
            }
        }

        @Override
        public void onBackPressed() {
            System.exit(1);
        }
    }

KOD XML LanguageChooserActivity

<?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:orientation="vertical"
    tools:context=".LanguageChooser">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="@color/colorAccent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>


    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:elevation="0dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabs"/>
    </com.google.android.material.appbar.AppBarLayout>



    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:id="@+id/pager"
        android:layout_height="match_parent" />



</LinearLayout>

Mój pasek narzędzi z widokiem wyszukiwania

<?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/action_Search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.widget.SearchView"
        />



</menu>

Kod XML pierwszej tabulatora (fragmentu) o nazwie hindi

<?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:orientation="vertical"

    tools:context=".Hindi">
<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/progressbar"
    android:visibility="visible"/>



    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

Kod Java Tab (fragment) o nazwie hindi

package com.piyushjaiswal.lyricswala;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.app.SearchManager;
import android.widget.ProgressBar;
import androidx.appcompat.widget.SearchView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class Hindi extends Fragment {

View v;
private RecyclerView myRecyclerview;
private List<Contact> listContact = new ArrayList<>();
   private FirebaseDatabase database = FirebaseDatabase.getInstance();
    private  DatabaseReference myRef = database.getReference();
    private  RecyclerViewAdapter recyclerViewAdapter;
    private ProgressBar progressBar;


public Hindi() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_hindi,container,false);
        progressBar= v.findViewById(R.id.progressbar);
        progressBar.setVisibility(View.VISIBLE);
        myRecyclerview =  v.findViewById(R.id.recyclerView);
        myRecyclerview.setHasFixedSize(true);
        myRecyclerview.setItemViewCacheSize(10);

        recyclerViewAdapter = new RecyclerViewAdapter(getContext(),listContact);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        myRecyclerview.setLayoutManager(layoutManager);


        myRecyclerview.setAdapter(recyclerViewAdapter);

        return  v;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        myRef.child("Hindi").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
                    listContact.add(dataSnapshot1.getValue(Contact.class));

                }
                recyclerViewAdapter.notifyDataSetChanged();

                progressBar.setVisibility(View.GONE);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {


Toast.makeText(getActivity(),databaseError.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

Adapter widoku do recyklingu

package com.piyushjaiswal.lyricswala;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>  {

    private Context mContext;
    private List<Contact> mData;
    private List<Contact> mDataFull;


    public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
        this.mContext = mContext;
        this.mData = mData;
        mDataFull = new ArrayList<>(mData);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v;
        v = LayoutInflater.from(mContext).inflate(R.layout.item_songs,parent,false);
        MyViewHolder vHolder= new MyViewHolder(v);
        return vHolder;


    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        holder.setData(mData.get(position).getName(),mData.get(position).getPhone(),mData.get(position).getUrl(),mData.get(position).getLyrics());

    }

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




    static class  MyViewHolder extends  RecyclerView.ViewHolder
{
 private TextView tv_name;
 private TextView tv_phone;
 private ImageView imageView;
    MyViewHolder(@NonNull View itemView) {
        super(itemView);

        tv_name = (TextView) itemView.findViewById(R.id.name_contact);
        tv_phone = (TextView) itemView.findViewById(R.id.phone_contact);
        imageView = (ImageView) itemView.findViewById(R.id.img_contact);
    }
    private void setData(final String name, final String phone, String url, final String Lyrics){
        Glide.with(itemView.getContext()).load(url).into(imageView);
        this.tv_phone.setText(phone);
        this.tv_name.setText(name);
        itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
             Intent intent = new Intent(itemView.getContext(),LyricsActivit.class);
             intent.putExtra("Lyrics",Lyrics);
             intent.putExtra("albumname",phone);
             intent.putExtra("songname",name);
             itemView.getContext().startActivity(intent);
            }
        });

    }

}

}

Zrzut ekranu mojej aktywnościWyjście aplikacji Chcę, aby powyższy widok wyszukiwania paska narzędzi działał na kartach (fragment) Mam nadzieję, że wyjaśnię mój problem

0
Piyush Jaiswal 16 grudzień 2019, 19:17
Może udostępnienie kodu może być pomocne
 – 
Bruno
16 grudzień 2019, 19:44

1 odpowiedź

Utwórz razem widok pagera i układ kart. zostanie rozwiązany.Tutaj jest przykładem.

0
mostafa tayebi 16 grudzień 2019, 20:02