Android Wear - adding an icon above CardFragment

I am trying to create a custom CardFragmentone where I have an icon that is above the map. Here is an example of what I'm trying to do:

desired result

So, I found two solutions for this.

Solution 1

Extension CardFragmentand redefinition onCreateContentViewas suggested in the answer to this question . Since it CardFramecontains a view, we can only see half the icon. At first I thought it was just because clipChildrenand clipToPaddingwere not set to false. So, I made a change for all ViewGroupparents ImageView, but it does not work. I had the same result.

I rejected this decision in order to concentrate on the following.

Decision 2

a Fragment CardScrollView CardFrame. ImageView CardFrame. FragmentGridPagerAdapter (page(0)(0),page(0)(1),page(1)(0)). getFragment , , CardScrollView CardFrame. :

wrong image

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 ?

+4

Source: https://habr.com/ru/post/1622289/


All Articles