如何将片段添加到选项卡式活动?

时间:2016-08-19 12:17:27

标签: android android-fragments android-tabs

如何将我的片段放入选项卡?我创建了一个片段来放置tabbedActivity的选项卡

这是我要放入选项卡

的片段
+------+-----------+--------+-------------+
| ID   | DRIVER_ID | STATUS | Total_count |
+------+-----------+--------+-------------+
|    3 |        22 |      1 |           3 |
|    5 |        22 |      1 |           3 |
|    6 |        22 |      1 |           3 |
+------+-----------+--------+-------------+

这是我的TabbedActivity

public class ownerFragment extends Fragment implements AsyncResponse, AdapterView.OnItemClickListener {
final String TAG = this.getClass().getName();
private ArrayList<Cars> carList;
private ListView lvCars;
private FunDapter<Cars> adapter;
private FunDapterFilter<Cars> filteredList;
SharedPreferences pref;
SharedPreferences.Editor editor;
private String LOG = "ownerhome";
TextView tvUser;


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.list_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    final Cars selectedCar = adapter. getItem(info.position);
    Log.d(LOG, selectedCar.Car_Model);

    if(item.getItemId() == R.id.menuUpdate){
        Intent in = new Intent(getActivity(), UpdateActivty.class);
        in.putExtra("cars", selectedCar);
        startActivity(in);
    }
    else if(item.getItemId()==R.id.menuDelete){
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setMessage("Are you sure to remove "+selectedCar.Car_Model +"?");
        alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                carList.remove(selectedCar);
                adapter.notifyDataSetChanged();

                HashMap postData = new HashMap();
                postData.put("Car_No", ""+selectedCar.Car_No);
                postData.put("mobile","android");


                PostResponseAsyncTask taskPost = new PostResponseAsyncTask(getActivity(), postData, new AsyncResponse() {
                    @Override
                    public void processFinish(String s) {
                        Log.d(TAG, s);
                        if (s.contains("success")) {
                            Toast.makeText(getActivity().getApplicationContext(), "Delete Successfully!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                taskPost.execute("http://carkila.esy.es/delete.php");
            }
        });
        alert.setNegativeButton("Cancel",null);
        alert.show();
    }
    return super.onContextItemSelected(item);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater findMenuItems = getActivity().getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.action_logout){
        editor = pref.edit();
        editor.clear();
        editor.commit();
        Intent in = new Intent(getActivity(), MainActivity.class);
        startActivity(in);
        getActivity().finish();
    }
    return super.onOptionsItemSelected(item);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_owner, container, false);

    ImageLoader.getInstance().init(UILConfig.config(getActivity()));

    lvCars = (ListView) v.findViewById(R.id.lvOnwer);

    pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);

    Log.d(TAG, pref.getString("username", ""));
    Log.d(TAG, pref.getString("password", ""));
    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);
    tvUser = (TextView) v.findViewById(R.id.tvUser);
    tvUser.setText("Hello! "+pref.getString("username", ""));

    taskRead.execute("http://carkila.esy.es/user.php?owner="+pref.getString("username", ""));

    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), InsertActivity.class);
            startActivity(in);
        }
    });
    registerForContextMenu(lvCars);
    return v;
}

@Override
public void processFinish(String s) {
    Log.d(TAG, s);
    carList = new JsonConverter<Cars>().toArrayList(s,Cars.class);

    BindDictionary<Cars> dict = new BindDictionary<Cars>();

    dict.addDynamicImageField(R.id.ivImg, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return product.Image;
        }
    }, new DynamicImageLoader() {
        @Override
        public void loadImage(String url, ImageView imageView) {

            ImageLoader.getInstance().displayImage(url, imageView); // Default options will be used

        }
    });
    dict.addStringField(R.id.tvCarModel, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Model: " + product.Car_Model;
        }
    });
    dict.addStringField(R.id.tvCarType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Type: " + product.Car_Type;
        }
    });

    dict.addStringField(R.id.tvCapacity, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Capacity: " + product.Capacity;
        }
    });
    dict.addStringField(R.id.tvFuelType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Fuel Type: " + product.FuelType;
        }
    });
    dict.addStringField(R.id.tvPlateNumber, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Plate Number: " + product.PlateNumber;
        }
    });
    dict.addStringField(R.id.tvPoster, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Posted by: " + product.owner;
        }
    });

    adapter = new FunDapter<>(
            getActivity(), carList, R.layout.layout_list, dict);
    lvCars.setAdapter(adapter);
    lvCars.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cars selectedCars = carList.get(position);
    Intent in = new Intent(getActivity(), OwnerDetail.class);
    in.putExtra("cars", selectedCars);
    startActivity(in);
}
}

谢谢你们! :d

2 个答案:

答案 0 :(得分:1)

我想出了如何制作它。

我刚刚在onCreateView

上插入了此代码
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1){
            View rootView = inflater.inflate(R.layout.content_ownerhome, container, false);
            return rootView;
        }
        else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
            View rootView = inflater.inflate(R.layout.content_insert, container, false);
            return rootView;
        }
        else if(getArguments().getInt(ARG_SECTION_NUMBER) == 3){
            View rootView = inflater.inflate(R.layout.content_detail, container, false);
            return rootView;
        }
        else {

            View rootView = inflater.inflate(R.layout.fragment_owner_tabs, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }

答案 1 :(得分:0)

假设您的Activity xml中有TabLayout,您需要在mViewPager.setAdapter(mSectionsPagerAdapter);之后输入此代码:

var tabs = (TabLayout)findViewById(R.id.{your tablayout id});
tabs.setupWithViewPager(mViewPager);