从活动切换到片段后,UI外观发生了变化

时间:2017-04-17 03:40:48

标签: android user-interface fragment

我有一个包含文本和绿色方块的Listview的活动。我终于改变主意并修改我的代码以使用片段而不是活动。但随后我的文字颜色改变了,我再也看不到绿色方块了。我没有更改列表代码中的任何内容或文本颜色,所以我不明白发生了什么。

感谢您的帮助。

enter image description here

MainActivity仅用作片段容器:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();

        Fragment fragment = fm.findFragmentById(R.id.ll_container);
        if (fragment == null) {
            fragment = new PollutionLevelsFragment();
            fm.beginTransaction()
                    .add(R.id.ll_container, fragment)
                    .commit();
        }
    }
}

MainActivity布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/ll_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

片段类

public class PollutionLevelsFragment extends Fragment implements PollutionLevelsFragmentMVP.View {
    private PollutionLevelAdapter plAdapter;

    @BindString(R.string.city)
    String city;

    @BindString(R.string.aqicn_token)
    String authToken;


    @BindView(R.id.lv_pollution_levels)
    ListView lvPollutionLevels;

    @Inject
    PollutionLevelsFragmentMVP.Presenter presenter;

    private ViewModel pollutionData;
    private ArrayList<String> testbidon;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pollution_levels, container, false);

        ButterKnife.bind(this,view);

        // Construct the data source
        ArrayList<PollutionLevel> arrayOfPollutionLevel = new ArrayList<>();
        // Create the adapter to convert the array to views
        plAdapter = new PollutionLevelAdapter(getActivity().getApplicationContext(), arrayOfPollutionLevel);

        lvPollutionLevels.setAdapter(plAdapter);
        return view;

    }

    @Override
    public void onStart() {
        super.onStart();

        presenter.setView(this);
        presenter.loadData(city, authToken);
    }

    @Override
    public void onStop() {
        super.onStop();
        presenter.rxUnsubscribe();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ((App) getActivity().getApplication()).getComponent().inject(this);
    }

    @Override
    public void updateData(ViewModel viewModel) {
        this.pollutionData = viewModel;

        ArrayList<PollutionLevel> pollutionLevels = viewModel.getAllPolluants();

        for(PollutionLevel pl : pollutionLevels) {
            plAdapter.add(pl);
        }
    }
}

片段布局

 <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView android:id="@+id/lv_pollution_levels"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

ListView的ArrayAdapter

public class PollutionLevelAdapter extends ArrayAdapter<PollutionLevel> {

@BindView(R.id.tv_name)
TextView tvName;

@BindView(R.id.tv_value)
TextView tvValue;

public PollutionLevelAdapter(Context context, ArrayList<PollutionLevel> pollutionLevels) {
    super(context,0,pollutionLevels);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_pollution, parent, false);
    }

    View view = convertView;

    ButterKnife.bind(this,view);
    PollutionLevel pollutionLevel = getItem(position);
    tvName.setText(pollutionLevel.getName());
    tvValue.setText(pollutionLevel.getValue());


    return view;
}
}

我的列表项

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<ImageView
    android:id="@+id/iv_warning"
    android:layout_width="20dp"
    android:layout_height="20dp"
    app:srcCompat="@drawable/rectangle"/>

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="center_vertical"
    android:text="TextView"/>

<TextView
    android:id="@+id/tv_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="center_vertical"
    android:text="TextView"/>

这是正确呈现Listview的Activity类的布局代码(所以在我从一个活动切换到一个片段之前)。我没有把Listview适配器的代码放在这里,因为它没有改变:

活动

public class PollutionLevelsActivity extends AppCompatActivity implements PollutionLevelsActivityMVP.View {
    private PollutionLevelAdapter plAdapter;

    @BindString(R.string.city)
    String city;

    @BindString(R.string.aqicn_token)
    String authToken;


    @BindView(R.id.lv_pollution_levels)
    ListView lvPollutionLevels;

    @Inject
    PollutionLevelsActivityMVP.Presenter presenter;

    private ViewModel pollutionData;
    private ArrayList<String> testbidon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((App) getApplication()).getComponent().inject(this);
        ButterKnife.bind(this);

    // Construct the data source
    ArrayList<PollutionLevel> arrayOfPollutionLevel = new ArrayList<>();
    // Create the adapter to convert the array to views
    plAdapter = new PollutionLevelAdapter(this, arrayOfPollutionLevel);

    lvPollutionLevels.setAdapter(plAdapter);

    }

    @Override
    protected void onStart() {
        super.onStart();

        presenter.setView(this);
        presenter.loadData(city, authToken);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.rxUnsubscribe();
    }

    @Override
    public void updateData(ViewModel viewModel) {
        this.pollutionData = viewModel;

        ArrayList<PollutionLevel> pollutionLevels = viewModel.getAllPolluants();

        for(PollutionLevel pl : pollutionLevels) {
            plAdapter.add(pl);
        }
    }
}

布局

<android.support.constraint.ConstraintLayout
    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"
    tools:context="com.livos.citypollution.pollutionlevels.PollutionLevelsActivity">

    <ListView
        android:id="@+id/lv_pollution_levels"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

要更改文字颜色,请添加此

  

android:textColor =“你的颜色代码”

//---when the SMS has been sent---
    getBaseContext().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "SMS Sent", "-")); //insert id, timesent, sms status into local database

                   //CALL Update OutBox Here

                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getApplicationContext(), "GENERIC FAILURE", Toast.LENGTH_SHORT).show();
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Generic Failure"));

                   //CALL Update OutBox Here

                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "No Service"));
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Null PDU"));

                    //CALL Update OutBox Here

                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Radio Off"));

                    //CALL Update OutBox Here

                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    getBaseContext().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getApplicationContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
                    co.insert(new outbox(1, strdate));
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "SMS Delivered", "-"));

                    //CALL Update OutBox Here

                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getApplicationContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Failed, SMS not sent"));

                    //CALL Update OutBox Here

                    break;
            }
        }
    }, new IntentFilter(DELIVERED));

答案 1 :(得分:0)

我正在接受。你给了一个适配器:

public class PollutionLevelAdapter extends ArrayAdapter<PollutionLevel> {

@BindView(R.id.tv_name)
TextView tvName;

@BindView(R.id.tv_value)
TextView tvValue;

public PollutionLevelAdapter(Context context, ArrayList<PollutionLevel> pollutionLevels) {
    super(context,0,pollutionLevels);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_pollution, parent, false);
    }

    View view = convertView;

    ButterKnife.bind(this,view);
    PollutionLevel pollutionLevel = getItem(position);
    tvName.setText(pollutionLevel.getName());
    tvValue.setText(pollutionLevel.getValue());


    return view;
}
}

您绑定了两个TextView而不是ImageView,因为您提供了list_item_pollution ImageView,因此绑定了您的ImageView:

@BindView(R.id.iv_warning)
ImageView iv_warning;

并在getView方法中设置为:

iv_warning.setBackgroundColor(Color.parseColor("your color code"));