این کلاسی ک تب لایوتم داخلش هست
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class menufrag extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menufrag);
ViewPager viewPager;
TabLayout tabLayout;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
setupviewpager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition())
{
case 0:
Toast.makeText(menufrag.this, "yes1", Toast.LENGTH_SHORT).show();
break;
case 1:
favfrag fav=new favfrag();
favfrag.new loadingdata2(menufrag.this).execute();
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void favloader() {
favfrag favfrag=new favfrag();
favfrag.new loadingdata2(getApplicationContext()).execute();
Toast.makeText(menufrag.this, "no", Toast.LENGTH_SHORT).show();
}
private void setupviewpager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addfrag(new listfrag(), " تمامی کالاها ");
adapter.addfrag(new favfrag(), "علاقه مندیها");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mfragment = new ArrayList<>();
private final List<String> mtittle = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mfragment.get(position);
}
@Override
public int getCount() {
return mfragment.size();
}
public void addfrag(Fragment fragment, String shet) {
mfragment.add(fragment);
mtittle.add(shet);
}
public CharSequence getPageTitle(int position) {
return mtittle.get(position);
}
}
{
}
}
این هم فرگمنتم هست
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.ViewDragHelper;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class favfrag extends Fragment {
RecyclerView rv;
static List<String> items2 = new ArrayList<>();
static List<String> imgurl2 = new ArrayList<>();
static List<String> companyid2 = new ArrayList<>();
static List<String> prices2 = new ArrayList<>();
static List<String> ids3 = new ArrayList<>();
SwipeRefreshLayout refreshLayout2;
private LinearLayoutManager manager;
static Fav_recy_adap adapterrecy2;
List<String> ids2 = new ArrayList<>();
String[] a={"5","7","8","2","5"};
JSONArray array;
View view;
public static int l;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Bundle bundle = getActivity().getIntent().getExtras();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_favfrag, container, false);
array=new JSONArray(Arrays.asList(a));
rv= (RecyclerView) view.findViewById(R.id.rvfavmenu);
manager = new LinearLayoutManager(getActivity().getApplicationContext());
// rv.setLayoutManager(manager);
refreshLayout2 = (SwipeRefreshLayout)view.findViewById(R.id.ref2);
refreshLayout2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText(getActivity(), "shet", Toast.LENGTH_SHORT).show();
items2.clear();
new loadingdata2(getActivity()).execute();
}
});
Internetchecker internet = new Internetchecker();
final boolean ischeck = internet.isconnected(getActivity());
if (ischeck) {
// new loadingdata2(getActivity().getBaseContext()).execute();
} else {
Toast.makeText(getActivity().getApplicationContext(), "اینترنتت روشن نیست مهندس ", Toast.LENGTH_SHORT).show();
}
return view;
}
public void load(){
new loadingdata2(getActivity().getApplicationContext()).execute();
}
class loadingdata2 extends AsyncTask {
private Context context;
// final private ProgressDialog pd = new ProgressDialog(getActivity());
StringBuilder sb = new StringBuilder();
String result;
public loadingdata2(Context context) {
this.context = context;
}
@Override
protected Object doInBackground(Object[] objects) {
String Link = "http://www.lovelyalone20ah.ir/bivasete/fav.php";
try {
String data = URLEncoder.encode("id", "UTF-8") + "=" + URLEncoder.encode(array.toString(), "UTF-8");
// data+="&"+ URLEncoder.encode("password","UTF-8")+"=" +URLEncoder.encode("","UTF-8");
URL mylink = new URL(Link);
URLConnection conect = mylink.openConnection();
conect.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conect.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject textlinkobj = jsonArray.getJSONObject(i);
String text = textlinkobj.getString("name");
String img = textlinkobj.getString("imageurl");
String company = textlinkobj.getString("companyid");
String price = textlinkobj.getString("price");
String id = textlinkobj.getString("id");
items2.add(text);
imgurl2.add(img);
companyid2.add(company);
prices2.add(price);
ids3.add(id);
}
} catch(Exception e)
{
e.printStackTrace();
// Toast.makeText(context, "مشکلی پیش اومده دوباره تلاش کن", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPreExecute() {
// pd.setMessage("وایسا");
// pd.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Object o) {
Fav_recy_adap adapterrecy2 = new Fav_recy_adap(context, items2, imgurl2,companyid2,prices2,ids2);
rv.setAdapter(adapterrecy2);
// pd.cancel();
// refreshLayout2.setRefreshing(false);
super.onPostExecute(o);
}
}
}