好的,所以我有一个带有这个按钮代码的section_home布局。
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text="Apply"
android:textColor="@color/white"
android:elevation="0dp"
android:id="@+id/button"
android:layout_alignTop="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我想要的是当有人点击&#34;申请&#34;按钮,它将转到我称为section_apply
的其他布局<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/window_bg"
android:clipToPadding="false"
android:fitsSystemWindows="true">
<ListView
android:id="@+id/launcherslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/light_bg"
android:divider="@color/transparent" />
这是我的家庭布局的java类,名为HomeFragment。
public class HomeFragment extends Fragment {
Button button;
private static final String MARKET_URL = "https://play.google.com/store/apps/details?id=";
private String PlayStoreDevAccount, PlayStoreListing, AppOnePackage, AppTwoPackage, AppThreePackage;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.section_home, container, false);
PlayStoreDevAccount = getResources().getString(R.string.play_store_dev_link);
PlayStoreListing = getActivity().getPackageName();
AppOnePackage = getResources().getString(R.string.app_one_package);
AppTwoPackage = getResources().getString(R.string.app_two_package);
AppThreePackage = getResources().getString(R.string.app_three_package);
ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (toolbar != null)
{
toolbar.setTitle(R.string.app_name);
}
ObservableScrollView content = (ObservableScrollView) root.findViewById(R.id.HomeContent);
TextView ratebtn = (TextView) root.findViewById(R.id.rate_button);
ratebtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent rate = new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URL + PlayStoreListing));
startActivity(rate);
}
});
//FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.apply_btn);
//fab.setColorNormal(getResources().getColor(R.color.accent));
//fab.setColorPressed(getResources().getColor(R.color.accent_pressed));
//fab.setColorRipple(getResources().getColor(R.color.semitransparent_white));
//fab.show(true);
//fab.attachToScrollView(content);
//fab.setOnClickListener(new View.OnClickListener() {
// @Override
//public void onClick(View v) {
// ((MainActivity) getActivity()).result.setSelectionByIdentifier(3);
//((MainActivity) getActivity()).switchFragment(3, getResources().getString(R.string.section_three), "Apply");
// }
//});
return root;
}
private boolean AppIsInstalled(String packageName)
{
final PackageManager pm = getActivity().getPackageManager();
boolean installed;
try
{
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e)
{
installed = false;
}
return installed;
}
}
这是我的名为ApplyFragment
的应用布局的java类public class ApplyFragment extends Fragment {
private static final String MARKET_URL = "https://play.google.com/store/apps/details?id=";
private String intentString;
private final List<Launcher> launchers = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.section_apply, container, false);
// Splits all launcher arrays by the | delimiter {name}|{package}
String[] launcherArray = getResources().getStringArray(R.array.launchers);
for (String launcher : launcherArray)
launchers.add(new Launcher(launcher.split("\\|")));
ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (toolbar != null)
toolbar.setTitle(R.string.section_three);
ListView launcherslist = (ListView) root.findViewById(R.id.launcherslist);
LaunchersAdapter adapter = new LaunchersAdapter(launchers);
launcherslist.setAdapter(adapter);
launcherslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (launchers.get(position).name.equals("Google Now Launcher"))
gnlDialog();
else if (LauncherIsInstalled(launchers.get(position).packageName))
openLauncher(launchers.get(position).name);
else
openInPlayStore(launchers.get(position));
}
});
return root;
}
private boolean LauncherIsInstalled(String packageName) {
final PackageManager pm = getActivity().getPackageManager();
boolean installed;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
private void openLauncher(String name) {
final String className = "com.jahirfiquitiva.paperboard" + ".launchers."
+ Character.toUpperCase(name.charAt(0))
+ name.substring(1).toLowerCase().replace(" ", "").replace("launcher", "")
+ "Launcher";
Class<?> cl = null;
try {
cl = Class.forName(className);
} catch (ClassNotFoundException e) {
Log.e("LAUNCHER CLASS MISSING", "Launcher class for: '" + name + "' missing!");
}
if (cl != null) {
Constructor<?> constructor = null;
try {
constructor = cl.getConstructor(Context.class);
} catch (NoSuchMethodException e) {
Log.e("LAUNCHER CLASS CONS",
"Launcher class for: '" + name + "' is missing a constructor!");
}
try {
if (constructor != null)
constructor.newInstance(getActivity());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void openInPlayStore(final Launcher launcher) {
intentString = MARKET_URL + launcher.packageName;
final String LauncherName = launcher.name;
final String cmName = "CM Theme Engine";
String dialogContent;
if (LauncherName.equals(cmName)) {
dialogContent = launcher.name + getResources().getString(R.string.cm_dialog_content);
intentString = "http://download.cyanogenmod.org/";
} else {
dialogContent = launcher.name + getResources().getString(R.string.lni_content);
intentString = MARKET_URL + launcher.packageName;
}
new MaterialDialog.Builder(getActivity())
.title(launcher.name + getResources().getString(R.string.lni_title))
.content(dialogContent)
.positiveText(R.string.lni_yes)
.negativeText(R.string.lni_no)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(intentString));
startActivity(intent);
}
}).show();
}
public class Launcher {
public final String name;
public final String packageName;
public Launcher(String[] values) {
name = values[0];
packageName = values[1];
}
}
class LaunchersAdapter extends ArrayAdapter<Launcher> {
final List<Launcher> launchers;
LaunchersAdapter(List<Launcher> launchers) {
super(getActivity(), R.layout.item_launcher, R.id.launchername, launchers);
this.launchers = launchers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
LauncherHolder holder;
if (item == null) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
item = inflater.inflate(R.layout.item_launcher, parent, false);
holder = new LauncherHolder(item);
item.setTag(holder);
} else {
holder = (LauncherHolder) item.getTag();
}
// Turns Launcher name "Something Pro" to "l_something_pro"
int iconResource = getActivity().getResources().getIdentifier(
"ic_" + launchers.get(position).name.toLowerCase().replace(" ", "_"),
"drawable",
getActivity().getPackageName()
);
holder.icon.setImageResource(iconResource);
holder.launchername.setText(launchers.get(position).name);
if (LauncherIsInstalled(launchers.get(position).packageName)) {
holder.isInstalled.setText(R.string.installed);
holder.isInstalled.setTextColor(getResources().getColor(R.color.green));
} else {
holder.isInstalled.setText(R.string.noninstalled);
holder.isInstalled.setTextColor(getResources().getColor(R.color.red));
}
return item;
}
class LauncherHolder {
final ImageView icon;
final TextView launchername;
final TextView isInstalled;
LauncherHolder(View v) {
icon = (ImageView) v.findViewById(R.id.launchericon);
launchername = (TextView) v.findViewById(R.id.launchername);
isInstalled = (TextView) v.findViewById(R.id.launcherinstalled);
}
}
}
private void gnlDialog() {
final String appLink = MARKET_URL + getResources().getString(R.string.extraapp);
new MaterialDialog.Builder(getActivity())
.title(R.string.gnl_title)
.content(R.string.gnl_content)
.positiveText(R.string.lni_yes)
.negativeText(R.string.lni_no)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
super.onPositive(dialog);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(appLink));
startActivity(intent);
}
}
).show();
}
}
基本上我想要的是家庭布局中的按钮带你到这个ApplyFragment / section_apply布局。
答案 0 :(得分:2)
这看起来像纸板模板,对吗?如果您将此代码放在HomeFragment中并且按钮的ID为&#34; button&#34;。
,此代码应该可以正常工作。 TextView btn = (TextView) root.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) getActivity()).result.setSelectionByIdentifier(3);
((MainActivity) getActivity()).switchFragment(3, getResources().getString(R.string.section_three), "Apply");
}
});
编辑:更改为与您的代码完美配合。
答案 1 :(得分:0)
在onClickListener中:
ApplyFragment applyFrag = new ApplyFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag, TAG_FRAGMENT)
.addToBackStack(null)
.commit();