我有一个FragmentPager用于实现3个选项卡。在其中一个标签中,我需要这样做: 如果用户点击按钮A,我想在标签内加载片段A,如果用户点击按钮B,我想在页面中加载片段B.
但我在第一阶段陷入困境。当我点击按钮时,onClick方法甚至不会被调用。我正在按照Slidenerd.com视频#10中关于Fragments的步骤进行操作。
我已经实现了onClicksListener并添加了onClick方法,就像示例一样。但Toast消息不会触发。
以下是我的MainActivity和我的父Fragment(提到的标签)和XML的代码。
提前致谢。
MainActivity.java
public class MainActivity extends ActionBarActivity implements
ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a {@link FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this
* becomes too memory intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(Farsi.Convert(getString(R.string.app_name_farsi)));
setContentView(R.layout.activity_main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding tab. We can also use ActionBar.Tab#select() to do this if we have a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter. Also specify this Activity object, which implements the TabListener interface, as the callback (listener) for when this tab is selected.
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
//ff code: To make the right most tab selected at the beginning.
mViewPager.setCurrentItem(mSectionsPagerAdapter.getCount()-1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will automatically handle clicks on the Home/Up button, so long as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
//******************************************************************** inner class
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0)
return QuickCreatePropertyListingFragment.newInstance(position+1);
else if (position ==1)
return SearchForRentFragment.newInstance(position+1);
else
return SearchForSaleFragment.newInstance(position+1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return Farsi.Convert(getString(R.string.label_create));
case 1:
return Farsi.Convert(getString(R.string.label_rent));
case 2:
return Farsi.Convert(getString(R.string.label_sell));
}
return null;
}
}
}
父片段
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.StatusUpdates;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class QuickCreatePropertyListingFragment extends Fragment implements OnClickListener{
private Activity activity;
public static QuickCreatePropertyListingFragment newInstance(int sectionNumber){
QuickCreatePropertyListingFragment fragment = new QuickCreatePropertyListingFragment();
Bundle args = new Bundle();
args.putInt("section_number", sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.quick_create_property_listing, container,false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Button here = (Button) getActivity().findViewById(R.id.radio32);
here.setOnClickListener(this);
}
public void onClick1(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity().getApplicationContext(), "this is my Toast message!!! =)" , Toast.LENGTH_LONG).show();
TextView tv = (TextView) getActivity().findViewById(R.id.textViewSale);
tv.setText("test");
Toast.makeText(getActivity().getApplicationContext(), "this is my Toast message!!! =)" , Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
}
}
activiti_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.myapp.MainActivity"/>
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.myapp.myapp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
child fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:text="category"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingRight="10dp">
<RadioGroup
android:id="@+id/radioGroupInTab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<RadioButton
android:id="@+id/radio31"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:background="@drawable/yourbuttonbackground"
android:button="@android:color/transparent"
android:checked="true"
android:gravity="center"
android:text="3" />
<RadioButton
android:id="@+id/radio32"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:background="@drawable/yourbuttonbackground"
android:button="@android:color/transparent"
android:checked="false"
android:gravity="center"
android:text="2"
android:onClick="onClick1"
/>
<RadioButton
android:id="@+id/radio33"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:background="@drawable/yourbuttonbackground"
android:button="@android:color/transparent"
android:checked="false"
android:gravity="center"
android:text=" 1"/>
</RadioGroup>
</HorizontalScrollView>
</LinearLayout>
答案 0 :(得分:0)
我认为您没有在父视图中注册片段视图。所以请试试这个代码。 我认为这将有效!
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//View rootView = inflater.inflate(R.layout.quick_create_property_listing, container,false);
View rootView = inflater.inflate(activity, R.layout.quick_create_property_listing, this);
return rootView;
}
答案 1 :(得分:0)
当我将其他人的监听器设置为RootView时,通常我会以这种方式编程。 请试试这个。
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.quick_create_property_listing, container,false);
TextView tv = rootView.findViewById(R.id.textViewSale);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// put your codes
}
});
return rootView;
}
答案 2 :(得分:0)
public void onClick(View arg0) {
// TODO Auto-generated method stub
onClick1(arg0);
}