扩充包含在另一个布局中的布局,其中包括其他布局

时间:2016-06-30 04:11:48

标签: android layout android-inflate getview

标题可能令人困惑,但这是我的情况,我有一个布局: get_my_sensors_activity.xml ,其中包括: app_bar_get_sensors.xml ,其中包括: content_get_my_sensors.xml 即可。在最后一个: content_get_my_sensors.xml 中,我有一个ListView,我想用 custom_item_sensors_list.xml 来扩充它,这是我自定义项目的布局。

我怎么能这样做,现在,我的getView实际上工作得很“好”,因为我知道它正在达到列表中的每个项目,但是在布局中只绘制了最后一个。

这是我的代码:

get_my_sensors_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_get_sensors"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_get_sensors.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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:fitsSystemWindows="true"
    tools:context="and.greenggers.smartdoo.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_get_my_sensors"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_get_my_sensors" />

</android.support.design.widget.CoordinatorLayout>

content_get_my_sensors.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@+id/sensorsLV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:divider="@color/white" />

</RelativeLayout>

这是我的活动

public class GetMySensors extends AppCompatActivity 
 implements NavigationView.OnNavigationItemSelectedListener, 
   AdapterView.OnItemClickListener {

private String mUserId;
private String mUserToken;
private ListView mList;
private List<Sensor> mGlobalSensorList = new ArrayList<>();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_my_sensors_activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_get_my_sensors);
    setSupportActionBar(toolbar);

    Utils.addDrawer(this);//Adds the drawer

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = 
    new (this,drawer,toolbar,R.string.navigation_drawer_open,
    R.string.navigation_drawer_close);           
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView)(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mList = (ListView) findViewById(R.id.sensorsLV);
    mList.setOnItemClickListener(this);

    new GetMySensorsAsync().execute();

}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    ///
}

private void setSensorListAdapter() {
    CustomSensorList mAdS = new 
          CustomSensorList(this, R.layout.custom_item_sensors_list,
                                                 mGlobalSensorList);
    mList.setAdapter(mAdS);
}

private class GetMySensorsAsync extends AsyncTask<> {

    @Override
    protected GetMySensorsResp doInBackground(Void... params) {
        // consult the web service and get the data
        return getMySensorsResp;
    }

    @Override
    protected void onPostExecute(GetMySensorsResp getMySensorsResp) {
        mGlobalSensorList = getMySensorsResp.getSensorsDataSDT();
        setSensorListAdapter();
    }
}

private class CustomSensorList extends ArrayAdapter<Sensor> {

    private Context context;
    private List<Sensor> mSensorList;

    public CustomSensorList(Context context,
                    int resource, List<Sensor> objects) {
        super(context, resource, objects);
        this.context = context;
        this.mSensorList = objects;
    }

    @Override
    public int getCount() {
        return mSensorList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);  
        View view = inflater.inflate(R.layout.custom_item_sensors_list, 
        parent, false);

        Sensor mSensor = mSensorList.get(position);
        TextView sensorName = (TextView)   
                                       view.findViewById(R.id.sensor_name);
        TextView sensorStatus = (TextView) 
                                       view.findViewById(R.id.sensor_value);

        sensorName.setText(mSensor.getSensorDataName());
        sensorStatus.setText(mSensor.getSensorValue());

        Log.i("get view", mSensor.toString());

        return view;
    }

  }
}

谢谢,我不知道它会是什么......我只能在嵌套布局中思考,并且在getView中获得正确的父级会有一些麻烦。

希望你能帮助我:)。

更新:这是logcat,打印getView和onItemClick方法:

06-30 10:12:02.309 16450-16450/and.greenggers.smartdoo I/get view:
Sensor{SensorId='947', SensorDataId='1', SensorDataName='DooSmart - EM01',
UserSensorMac='5c:cf:7f:81:a2:28', SensorValue='1593294', 
UserSensorDate='2016-06-30T15:11:22'}

06-30 10:12:02.312 16450-16450/and.greenggers.smartdoo I/get view:
Sensor{SensorId='1', SensorDataId='1', SensorDataName='DooSmart - EM01', 
UserSensorMac='65162132165549516123', SensorValue='123', 
UserSensorDate='2016-06-29T23:11:34'}

06-30 10:12:10.854 16450-16450/and.greenggers.smartdoo I/click item: 
Sensor{SensorId='1', SensorDataId='1', SensorDataName='DooSmart - EM01', 
UserSensorMac='65162132165549516123', SensorValue='123', 
UserSensorDate='2016-06-29T23:11:34'}

解决了@MikeM我在布局中错过了xmlns:app =“http://schemas.android.com/apk/res-auto”和app:layout_behavior =“@ string / appbar_scrolling_view_behavior” content_get_my_sensors.xml ...修复它是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">

<ListView
    android:id="@+id/sensorsLV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:clickable="true"
    android:divider="@color/white" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

像这样更改您的布局..

  

<强> get_my_sensors_activity.xml

$date = '2016-01-01';
$y = date('Y', strtotime("$date"));
$x = $y;
while(1){
    $date = date('Y-m-d', strtotime("next Monday $date"));
    $y = date('Y', strtotime("$date")); 
    if($x != $y) break;
        echo $date."<br/>"; 
}

只有你的抽屉布局改变像这样。它工作得很好......