2
我怎樣才能把我的片段加入標籤?我創建了一個片段穿上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);
}
}
的片段,這裏是我的TabbedActivity
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
的可能的複製該代碼[如何創建頁面刷卡查看Android的選項卡式風格(http://stackoverflow.com/questions/22265796/how-to-創建-Android的標籤的風格,與頁划動視) –