我怎样才能把碎片放到标签上?我创建了一个片段放在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);
}
}
这是我的活动表
public class tabActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if ( getArguments().getInt(ARG_SECTION_NUMBER) == 1){
View rootView = inflater.inflate(R.layout.fragment_owner, container, false);
return rootView;
}
else {
View rootView = inflater.inflate(R.layout.fragment_tab, 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;
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Your Cars";
case 1:
return "Rented";
case 2:
return "GPS Tracker";
}
return null;
}
}
}
谢谢你们!D
最佳答案
我知道怎么做了。
我刚刚在我的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;
}
关于android - 如何将 fragment 添加到选项卡式 Activity ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39038883/