我想從ViewHolder類開始意圖到導航抽屜片段類的片段。請參閱下面的代碼。如何將Recycleview項目移動到導航抽屜的片段?
public class LinearViewHolder extends RecyclerView.ViewHolder {
public TextView countryName;
public ImageView countryPhoto;
public Context context;
public LinearViewHolder(View v) {
super(v);
context = itemView.getContext();
countryName = (TextView) itemView.findViewById(R.id.country_name);
countryPhoto = (ImageView) itemView.findViewById(R.id.country_photo);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == 0) {
//here we go to ActivityFragment class???
} else if (pos == 1) {
//here we go to ActivityFragment class???
} else if (pos == 2) {
//here we go to ActivityFragment class???
} else if (pos == 3) {
} else if (pos == 4) {
} else if (pos == 5) {
}
}
});
}
}
而FragmentActivity.class代碼是下面這個!
public class ActivityFragment extends Fragment {
private List<ActivitySetData> history;
RecyclerView recList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.actvity_toolbar, container, false);
Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar_two);
setSupportActionBar(toolbar);
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) v.findViewById(R.id.collapsing_toolbar);
AppBarLayout appBarLayout = (AppBarLayout) v.findViewById(R.id.appbar);
appBarLayout.setExpanded(true);
// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.app_name));
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
try {
Glide.with(this).load(R.color.light_blue).into((ImageView) v.findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
recList = (RecyclerView) v.findViewById(R.id.recycler_view);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
initializeData();
initializeAdapter();
return v;
}
private void initializeData() {
history = new ArrayList<>();
history.add(new ActivitySetData(R.color.light_blue, "Getting knowing the Activity", getString(R.string.activity_first)));
history.add(new ActivitySetData(R.color.light_blues1, "Navigation Through Activities", getString(R.string.activity_second)));
history.add(new ActivitySetData(R.color.light_blues2, "Tasks", getString(R.string.activity_third)));
history.add(new ActivitySetData(R.color.light_blues3, "The Talkback Stack", getString(R.string.activity_four)));
history.add(new ActivitySetData(R.color.light_blues4, "Activity Life Cycle", getString(R.string.activity_five)));
history.add(new ActivitySetData(R.color.light_blues6, "Activity Life Cycle Methods", getString(R.string.activity_six)));
}
private void initializeAdapter() {
ActivityAdapter adapter = new ActivityAdapter(history);
recList.setAdapter(adapter);
}
private void setSupportActionBar(Toolbar toolbar) {
}
}
和我父的代碼MainActivity.class低於
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Defining Variables
private NavigationView navigationView;
private DrawerLayout drawerLayout;
String showFragmentCheck;
private TextView tool;
FragmentManager mFragmentManager;
LinearLayout linearLayoutSelectLocation;
private TextView textViewCancel;
private TextView textViewLike;
private TextView textViewShare;
private ImageView ic_short;
private FloatingActionButton fab;
private static final String SHOWCASE_ID = "sequence example";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register_view();
presentShowcaseSequence(); // one second delay
textViewLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=app.mytutoandroid"));
startActivity(browserIntent);
}
});
textViewShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=app.mytutoandroid");
startActivity(intent);
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
textViewCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayoutSelectLocation.setVisibility(View.INVISIBLE);
}
});
ic_short.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, UiShortcuts.class);
// intent.putExtra("lastFragment","News");
startActivity(intent);
finish();
}
});
mFragmentManager = getSupportFragmentManager();
showFragmentCheck = getIntent().getStringExtra("checkFragment");
if (showFragmentCheck == null) {
showChildFragment("Getting Started");
} else {
showChildFragment(showFragmentCheck);
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawerLayout.closeDrawers();
showChildFragment(menuItem.getTitle().toString());
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name,
R.string.app_name);
drawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
public void register_view() {
tool = (TextView) findViewById(R.id.toolbar_title);
tool.setOnClickListener(this);
textViewLike = (TextView) findViewById(R.id.textViewLike);
textViewShare = (TextView) findViewById(R.id.textViewShare);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
textViewCancel = (TextView) findViewById(R.id.textViewCancel);
linearLayoutSelectLocation = (LinearLayout) findViewById(R.id.linearLayoutSelectLocation);
ic_short = (ImageView) findViewById(R.id.choose_short);
ic_short.setOnClickListener(this);
}
public void showChildFragment(String fragmentTitle) {
if (fragmentTitle.equals("Activity")) { // on 2nd item in the menu, do somethin
tool.setText("Intro to Activity");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new ActivityFragment()).commit();
} else if (fragmentTitle.equals("Getting Started")) { // on 2nd item in the menu, do somethin
tool.setText("Android Development");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new MainFragment()).commit();
} else if (fragmentTitle.equals("Services")) { // on 2nd item in the menu, do somethin
tool.setText("Intro to Services");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new ServicesFragment()).commit();
} else if (fragmentTitle.equals("Content Provider")) { // on 2nd item in the menu, do somethin
tool.setText("Intro to Content Provider");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new ContentFragment()).commit();
} else if (fragmentTitle.equals("BroadCast Receiver")) { // on 2nd item in the menu, do somethin
tool.setText("Intro to BroadCast Receiver");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new BroadCastFragment()).commit();
} else if (fragmentTitle.equals("Networking")) { // on 2nd item in the menu, do somethin
tool.setText("Intro to Networking");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new NetworkingFragment()).commit();
} else if (fragmentTitle.equals("Android Manifest")) { // on 2nd item in the menu, do somethin
tool.setText("Intro to Manifests");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new ManiFragment()).commit();
} else if (fragmentTitle.equals("References")) { // on 2nd item in the menu, do somethin
tool.setText("References");
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new References()).commit();
} else {
showChildFragment("Getting Started");
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.choose_short || v.getId() == R.id.toolbar_title) {
presentShowcaseSequence();
}
}
private void presentShowcaseSequence() {
ShowcaseConfig config = new ShowcaseConfig();
config.setDelay(500); // half second between each showcase view
MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(this, SHOWCASE_ID);
sequence.setOnItemShownListener(new MaterialShowcaseSequence.OnSequenceItemShownListener() {
@Override
public void onShow(MaterialShowcaseView itemView, int position) {
Toast.makeText(itemView.getContext(), "Item #" + position, Toast.LENGTH_SHORT).show();
}
});
sequence.setConfig(config);
sequence.addSequenceItem(ic_short, "Shortcut for Android topics\nFind the Android Content with details.", "GOT IT");
sequence.start();
}
}
轉到其父活動沒有片段,並從那裏管理它 – Nisarg
在這種情況下不可能嗎? –
是的,這是可能的,用不同的意圖值管理 – Nisarg