我正在尝试创建一个自定义CardFragment
,其中我有一个位于卡片上方的图标。以下是我正在尝试做的一个例子:
所以我发现了两种可能的解决方案。
解决方案1
扩展CardFragment
并覆盖此question答案中提出的onCreateContentView
。由于CardFrame
包含视图,因此我们只能看到图标的一半。起初,我认为这只是因为clipChildren
和clipToPadding
未设置为false。所以,我对ViewGroup
的所有ImageView
父母进行了更改,但似乎没有效果。我有同样的结果。
我放弃了这个解决方案,专注于下一个。
解决方案2
扩展Fragment
并使用CardScrollView
和CardFrame.
使用此解决方案,我可以将ImageView
放在CardFrame
之外。它首次执行FragmentGridPagerAdapter
(page(0)(0),page(0)(1),page(1)(0))
的加载页面时效果很好。滚动到另一个页面后调用getFragment
时,CardScrollView
似乎不会展开CardFrame
。结果如下:
MainActivity:
package cardfragment.test.com.cardfragmentexample;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wearable.view.BoxInsetLayout;
import android.support.wearable.view.GridViewPager;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class MainActivity extends WearableActivity {
private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
new SimpleDateFormat("HH:mm", Locale.US);
private BoxInsetLayout mContainerView;
private MontreGridPagerAdapter mAdapter;
private GridViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
mContainerView = (BoxInsetLayout) findViewById(R.id.container);
mPager = (GridViewPager) findViewById(R.id.pager);
mAdapter = new MontreGridPagerAdapter(getFragmentManager());
mPager.setAdapter(mAdapter);
}
@Override
public void onEnterAmbient(Bundle ambientDetails) {
super.onEnterAmbient(ambientDetails);
updateDisplay();
}
@Override
public void onUpdateAmbient() {
super.onUpdateAmbient();
updateDisplay();
}
@Override
public void onExitAmbient() {
updateDisplay();
super.onExitAmbient();
}
private void updateDisplay() {
if (isAmbient()) {
mContainerView.setBackgroundColor(getResources().getColor(android.R.color.black));
} else {
mContainerView.setBackground(null);
}
}
}
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/black">
<android.support.wearable.view.GridViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.wearable.view.BoxInsetLayout>
FragmentGridPagerAdapter
:
package cardfragment.test.com.cardfragmentexample;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.wearable.view.FragmentGridPagerAdapter;
public class MontreGridPagerAdapter extends FragmentGridPagerAdapter
{
public MontreGridPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getFragment(int row, int col)
{
return MyFragment.newInstance();
}
@Override
public int getRowCount()
{
return 4;
}
@Override
public int getColumnCount(int i) {
return 3;
}
}
自定义片段:
package cardfragment.test.com.cardfragmentexample;
import android.app.Fragment;
import android.os.Bundle;
import android.support.wearable.view.CardFrame;
import android.support.wearable.view.CardScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MyFragment extends Fragment {
public final static String TAG = MyFragment.class.getSimpleName();
private ImageView mIcon2;
private ImageView mIcon1;
private TextView mTitle;
private TextView mDescription;
private CardScrollView mCardScrollView;
private CardFrame mCardFrame;
public static MyFragment newInstance() {
MyFragment fragment = new MyFragment();
final Bundle args = new Bundle(1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vue = inflater.inflate(R.layout.fragment_my, container, false);
mIcon2 = (ImageView) vue.findViewById(R.id.icon2);
mIcon1 = (ImageView) vue.findViewById(R.id.icon1);
mTitle = (TextView) vue.findViewById(R.id.title);
mDescription = (TextView) vue.findViewById(R.id.description);
mCardScrollView = (CardScrollView) vue.findViewById(R.id.card_scroll_view);
mCardFrame = (CardFrame) vue.findViewById(R.id.card_frame);
/*mCardFrame.setExpansionEnabled(true);
mCardFrame.setExpansionDirection(1);
mCardFrame.setExpansionFactor(10.0F);
mCardScrollView.setExpansionEnabled(true);
mCardScrollView.setExpansionDirection(1);
mCardScrollView.setExpansionFactor(10.0F);*/
mIcon1.setImageResource(R.drawable.icon1);
mIcon2.setImageResource(R.drawable.icon2);
mTitle.setText("Title");
mDescription.setText("My description");
return vue;
}
}
fragment_my.xml
:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.wearable.view.CardScrollView
android:id="@+id/card_scroll_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.wearable.view.CardFrame
android:id="@+id/card_frame"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="bottom">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_info"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/title" />
<ImageView
android:id="@+id/icon1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:layout_box="all">
<ImageView
android:id="@+id/icon2"
android:layout_width="30dp"
android:layout_height="30dp"/>
</RelativeLayout>
</android.support.wearable.view.BoxInsetLayout>
</RelativeLayout>
我尝试设置其他人建议的扩展方向和因素,但我遇到了同样的问题。我错过了能够在其他页面中正确展开CardFrame
的内容吗?