import android.os.Bundle;
import android.support.v4.app.Fragment;
public class TestFragment extends Fragment {
public static TestFragment newInstance(int num, String title) {
TestFragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putInt("num", num);
args.putString("title", title);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int num = getArguments().getInt("num", 0);
String title = getArguments().getString("title", "");
}
}
你可以在Activity里,简单的加载一个带参数的Fragment:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
TestFragment fragment = TestFragment.newInstance(5, "fragment title");
ft.replace(R.id.placeholder, fragment);
ft.commit();
public class TestFragment extends Fragment {
public void doSomething(String param) {
// do something in fragment
}
}
在Activity中,可以直接通过Fragment的对象句柄调用该方法:
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestFragment testFragment = new TestFragment();
testFragment.doSomething("some param");
}
}
Fragment Listener 如果Fragment需要共享事件给Activity,则需要利用这个方法。Fragment中定义一个接口,并且由Activity来实现这个接口。在onAttach()方法中将实现了这个接口的Activity获得到。
在Fragment中定义接口代码如下:
import android.support.v4.app.Fragment;
public class MyListFragment extends Fragment {
// ...
// Define the listener of the interface type
// listener is the activity itself
private OnItemSelectedListener listener;
// Define the events that the fragment will use to communicate
public interface OnItemSelectedListener {
public void onRssItemSelected(String link);
}
// Store the listener (activity) that will have events fired once the fragment is attached
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implement MyListFragment.OnItemSelectedListener");
}
}
// Now we can fire the event when the user selects something in the fragment
public void onSomeClick(View v) {
listener.onRssItemSelected("some link");
}
}
在Activity中实现这个接口:
import android.support.v4.app.FragmentActivity;
public class RssfeedActivity extends FragmentActivity implements
MyListFragment.OnItemSelectedListener {
DetailFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
fragment = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailFragment);
}
// Now we can define the action to take in the activity when the fragment event fires
@Override
public void onRssItemSelected(String link) {
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}
}
}
本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ...
[详细]
Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ...
[详细]