[英]I implemented card views in XML on Android Studios. How can I have this act as buttons?
I'm new to Android and recently created a dashboard for my app. 我是Android的新手,最近为我的应用创建了一个仪表板。 I followed a tutorial but it only covered the front end.
我遵循了一个教程,但它只涵盖了前端。 I created a layout called content_main.xml with three Cardviews.
我用三个Cardview创建了一个名为content_main.xml的布局。 Each of these Cardviews contains the code
这些Cardview均包含以下代码
android:clickable="true"
Beginning the back end of things, I created a java receiver class called DashboardActivity (as seen below). 从后端开始,我创建了一个名为DashboardActivity的Java接收器类(如下所示)。 I want to make my Cardviews clickable so that when one is clicked, I am taken to another screen within my app.
我想让我的Cardviews可点击,以便在单击某个Cardviews时将其带到应用程序中的另一个屏幕。 The other screens in my app I am trying to get to are fragments.
我尝试访问的应用程序中的其他屏幕是片段。 Each of these screens has its own java class in my "Fragments" folder.
这些屏幕中的每一个在我的“ Fragments”文件夹中都有自己的java类。 One of them, of which I try calling on in the code that follows is called SettingsViewFragment() .
其中之一,我尝试在下面的代码中调用,称为SettingsViewFragment() 。
So far, I have tried the following. 到目前为止,我已经尝试了以下方法。 It does nothing.
它什么也没做。
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();
}
});
}
}
UPDATE: FULL CODE 更新:完整代码
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");
}
});
}
}
And my XML: (it continues for 3 cards this way) 我的XML :(这种方式可连续3张卡)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.newproject.DashboardActivity"
tools:showIn="@layout/toolbar">
<FrameLayout
android:id="@+id/dashframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/dash_settings"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@drawable/bg_circle1"
android:src="@drawable/ic_settingspicture"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:text="View your settings"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Follow along with guided tutorials"
android:padding="5dp"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
Replace 更换
Fragment fragment1 = new SettingsViewFragment();
With this 有了这个
Fragment fragment1 = new SettingsViewFragment();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Main_Layout,fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Change Fragment fragment1 = new SettingsViewFragment ();
更改
Fragment fragment1 = new SettingsViewFragment ();
by Toast.makeText (mActivity, "onCLick", Toast.LENGTH_LONG) .show ();
通过
Toast.makeText (mActivity, "onCLick", Toast.LENGTH_LONG) .show ();
//if show toast The problem is not the onClick, the problem is when loading the fragment //如果显示烤面包,问题不是onClick,问题是加载片段时
Fragments should be instantiated with the newInstance()
static method. 片段应使用
newInstance()
静态方法实例化。
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();
}
});
In the SettingsFragment 在设置片段中
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
XML for content_main 用于content_main的XML
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>
This will transact the fragment into the FrameLayout that is positioned below the CardViews. 这会将片段处理到CardViews下方的FrameLayout中。 Now this is probably not ideal, you would most likely want to launch a new activity that will host the fragment but this should work.
现在这可能并不理想,您很可能希望启动一个新活动来托管该片段,但这应该可以工作。 Unless this is already in some Activity that hosts the fragment.
除非已经在承载该片段的某些Activity中使用了它。 But the key code is in the
onClick
. 但是关键代码在
onClick
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.