Java 我在Android Studio上用XML实现了卡片视图。我怎么能把这个当按钮用呢?
我是Android新手,最近为我的应用程序创建了一个仪表板。我遵循了一个教程,但它只涵盖了前端。我创建了一个名为content\u main.xml的布局,其中包含三个CardView。每个CardView都包含代码Java 我在Android Studio上用XML实现了卡片视图。我怎么能把这个当按钮用呢?,java,android,android-fragments,android-cardview,Java,Android,Android Fragments,Android Cardview,我是Android新手,最近为我的应用程序创建了一个仪表板。我遵循了一个教程,但它只涵盖了前端。我创建了一个名为content\u main.xml的布局,其中包含三个CardView。每个CardView都包含代码 android:clickable="true" 从后端开始,我创建了一个名为DashboardActivity的java接收器类(如下所示)。我想让我的Cardviews可点击,这样当一个被点击时,我会被带到我应用程序中的另一个屏幕。在我的应用程序中,我试图访问的其他屏幕都是片
android:clickable="true"
从后端开始,我创建了一个名为DashboardActivity的java接收器类(如下所示)。我想让我的Cardviews可点击,这样当一个被点击时,我会被带到我应用程序中的另一个屏幕。在我的应用程序中,我试图访问的其他屏幕都是片段。每个屏幕在我的“片段”文件夹中都有自己的java类。我在下面的代码中尝试调用的其中一个函数名为setingsviewfragment()
到目前为止,我已经尝试了以下方法。它什么也不做
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = (CardView) findViewById(R.id.dash_settings);
cd2 = (CardView) findViewById(R.id.dash_profile);
cd3 = (CardView) findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment1 = new SettingsViewFragment();
}
});
}
}
更新:完整代码
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = findViewById(R.id.dash_settings);
cd2 = findViewById(R.id.dash_profile);
cd3 = findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Testing 123 123 123");
}
});
}
}
还有我的XML:(以这种方式继续3张卡)
Change
fragment1=新设置viewfragment()
byToast.makeText(mActivity,“onCLick”,Toast.LENGTH\u LONG.show()代码>
//如果问题不是onClick,则问题在于加载片段时替换
Fragment fragment1 = new SettingsViewFragment();
用这个
Fragment fragment1 = new SettingsViewFragment();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Main_Layout,fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
应该使用newInstance()
static方法实例化片段
cd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment settingsFragment = SettingsFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.contentView, settingsFragment);
fragmentTransaction.commit();
}
});
在设置片段中
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
内容的XML_main
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/dash_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/dash_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/dash_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<FrameLayout
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
</FrameLayout>
</LinearLayout>
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
xmlns:app=”http://schemas.android.com/apk/res-auto"
android:orientation=“vertical”>
好啊问题在于onClick,而不是片段。我想我需要创建一个适配器类并删除我当前使用的这个接收方类。这就是我一直在读的…“应该用newInstance()静态方法实例化片段。”不是真的。你可以为自己的方便而做。但这不是一个规则,虽然技术上是正确的,但这是一个好的实践,也是谷歌推荐的实践,因为系统是如何使用片段构造函数的。这也是可靠地将数据传递给片段的唯一方法。看看这篇解释它的文章:读了这篇文章,我现在同意你的观点。明天将重写所有非静态实例化:)这会编译,对我来说更有意义,但仍然没有发生任何事情。我一点也不能去上班。您认为我需要将receiver类中的此代码移到adapter类吗?您可以将其移到任何位置,我将亲自处理recyclerview on adapter的所有on item click listener。但它在这里也应该起作用。尝试在OnClick内打印日志,我可能会尝试。它在接收器类中根本不起作用。。我尝试使用日志输出进行测试,但什么也没发生!显示您的完整代码,然后它将帮助我们找出到底是什么问题isokay我已经编辑了我的问题,包括我的完整代码现在。Onclick仍然不起作用
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/dash_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/dash_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/dash_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<FrameLayout
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
</FrameLayout>
</LinearLayout>