我想从ViewHolder类开始意图到Fragment of Navigation Drawer Fragment类。请参阅下面的代码。
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) {
}
}
我的Parent 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();
}
}
答案 0 :(得分:1)
在适配器中
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == 0) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("checkFragment", "ActivityFragment");
context.startActivity(intent);
} else if (pos == 1) {
//same as above
} else if (pos == 2) {
///same as above
} else if (pos == 3) {
} else if (pos == 4) {
} else if (pos == 5) {
}
}
});
在MainActivity中
mFragmentManager = getSupportFragmentManager();
showFragmentCheck = getIntent().getStringExtra("checkFragment");
if (showFragmentCheck == null) {
showChildFragment("Getting Started");
} else {
if (showFragmentCheck.equalsIgnoreCase("ActivityFragment")) {
showChildFragment("Activity");
}else
{
showChildFragment(showFragmentCheck);
}
}
答案 1 :(得分:0)
尝试这种方式或尝试使用此链接Click Here to more
<android.support.v7.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
依赖性: -
编译'com.android.support:recyclerview-v7:21.0.0-rc1'
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
}
public class ContactInfo {
protected String name;
protected String surname;
protected String email;
protected static final String NAME_PREFIX = "Name_";
protected static final String SURNAME_PREFIX = "Surname_";
protected static final String EMAIL_PREFIX = "email_";
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
protected TextView vEmail;
protected TextView vTitle;
public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vEmail = (TextView) v.findViewById(R.id.txtEmail);
vTitle = (TextView) v.findViewById(R.id.title);
}
}
public class ContactAdapter extends
RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<ContactInfo> contactList;
public ContactAdapter(List<ContactInfo> contactList) {
this.contactList = contactList;
}
@Override
public int getItemCount() {
return contactList.size();
}
@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vEmail.setText(ci.email);
contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_layout, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
...
}
}