我有ControllerFragment
,其中有3个子片段。通过ViewPager在tabLayout中提供子代码片段更改。 Unsend Apple和Banana徽章计数是可计算的变量值。 unSendAppleCountTxtView
和unSendBananaCountTxtView
根据这些变量值进行更改。
我的函数正在运行主线程。
尝试过的方法:
setUnSendAppleCount
函数在这些线程中运行(runOnUIThread,new Handler(),new Handler(Looper.getMainLooper()))AsyncTask
而没有在postExecute
中使用setView和setText。所有方法都不起作用。 TextView是空值。
public class ControllerFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.controller_fragment, viewGroup, false);
tabs = ViewUtil.findById(rootView, R.id.tabs);
viewPager = ViewUtil.findById(rootView, R.id.controllerViewPager);
setFragments();
setupViewPager(viewPager);
tabs.setupWithViewPager(viewPager);
setupTabIcons(viewGroup);
return rootView;
}
private void setupTabIcons(ViewGroup viewGroup) {
LinearLayout appleListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
RelativeLayout pearListTab = (RelativeLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text, viewGroup, false);
LinearLayout bananaListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
unSendAppleCountTxtView = appleListTab.findViewById(R.id.badgeView);
unSendBananaCountTxtView = bananaListTab.findViewById(R.id.badgeView);
TextView appleTextView = appleListTab.findViewById(R.id.textView);
TextView pearTextView = pearListTab.findViewById(R.id.textView);
TextView bananaTextView = bananaListTab.findViewById(R.id.textView);
setUnSendAppleCount(2); //....this function is not working
setUnSendBananaCount(2); //.....this function is not working
bananaTextView.setText(getString(R.string.bananas));
tabs.getTabAt(0).setCustomView(bananaListTab);
appleTextView.setText(getString(R.string.apples));
tabs.getTabAt(1).setCustomView(appleListTab);
pearTextView.setText(getString(R.string.pears));
tabs.getTabAt(2).setCustomView(pearListTab);
}
private void setupViewPager(ViewPager viewPager) {
TabsViewPagerAdapter adapter = new TabsViewPagerAdapter(getFragmentManager());
adapter.addFrag(bananaFragment, getString(R.string.bananas));
adapter.addFrag(appleFragment, getString(R.string.apples));
adapter.addFrag(pearFragment, getString(R.string.pears));
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1);
}
private void setUnSendAppleCount(int unSendCount) {
if (unSendAppleCountTxtView != null) {
if (unSendCount > 0) {
unSendAppleCountTxtView.setVisibility(View.VISIBLE);
unSendAppleCountTxtView.setText(String.format(getMyLocale(), "%d", unSendCount));
} else {
unSendAppleCountTxtView.setVisibility(View.GONE);
}
}
}
}
controller_fragment 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tab_unpressed_color">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/tabColor" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/controllerViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/app_bar_layout" />
</RelativeLayout>
tab_title_text_and_badge.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:weightSum="2"
android:orientation="horizontal"
tools:background="@color/Black">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:layout_gravity="center"
android:maxLines="1"
android:layout_weight="1.99"
android:textColor="@color/white"
android:textSize="@dimen/infoTitle"/>
<TextView
android:id="@+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="@drawable/tab_controller_badge"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/miniTextSize"
android:visibility="gone"
tools:text="11"
tools:visibility="visible" />
</LinearLayout>
请帮帮我
答案 0 :(得分:2)
尝试setVisibility android:visibility="visible"
或android:visibility="invisible"
删除gone
属性,删除TextView。
<TextView
android:id="@+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="@drawable/tab_controller_badge"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/miniTextSize"
android:visibility="visible" //change here
tools:text="11"
tools:visibility="visible" />
答案 1 :(得分:2)
请使用android:text
代替tools:text
答案 2 :(得分:2)
尝试使用以下xml,
<?xml version="1.0" encoding="UTF-8"?>
<!--
JBoss, Home of Professional Open Source
Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
contributors by the @authors tag. See the copyright.txt in the
distribution for a full listing of individual contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<profiles>
<!-- This section contains the properties that may vary from environment to environment.
They are activated via profiles. -->
<!-- Properties for use of variables, which shall be replaced by real values before deployment -->
<profile>
<id>var</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<isp.rin.ds.url>@@isp.rin.ds.url@@</isp.rin.ds.url>
<isp.rin.ds.user>@@isp.rin.ds.user@@</isp.rin.ds.user>
<isp.rin.ds.pwd>@@isp.rin.ds.pwd@@</isp.rin.ds.pwd>
<isp.rin.ds.host>@@isp.rin.ds.host@@</isp.rin.ds.host>
<isp.rin.ds.port>@@isp.rin.ds.port@@</isp.rin.ds.port>
<isp.rin.ds.db>@@isp.rin.ds.db@@</isp.rin.ds.db>
<isp.rin.activemq.transaction-support>@@isp.rin.activemq.transaction-support@@</isp.rin.activemq.transaction-support>
<isp.rin.activemq.server-url.primary>@@isp.rin.activemq.server-url.primary@@</isp.rin.activemq.server-url.primary>
<isp.rin.activemq.server-url.secondary>@@isp.rin.activemq.server-url.secondary@@</isp.rin.activemq.server-url.secondary>
<isp.rin.logg.level>@@isp.rin.logg.level@@</isp.rin.logg.level>
<isp.rin.business.logg.level>@@isp.rin.business.logg.level@@</isp.rin.business.logg.level>
<isp.rin.message.logg.level>@@isp.rin.message.logg.level@@</isp.rin.message.logg.level>
</properties>
</profile>
<!-- Properties for local developer machine -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<isp.rin.ds.url>jdbc:edb://localhost:37450/rin</isp.rin.ds.url>
<isp.rin.ds.user>enterprisedb</isp.rin.ds.user>
<isp.rin.ds.pwd>admin</isp.rin.ds.pwd>
<isp.rin.ds.host>localhost</isp.rin.ds.host>
<isp.rin.ds.port>37450</isp.rin.ds.port>
<isp.rin.ds.db>dss</isp.rin.ds.db>
<isp.rin.activemq.transaction-support>LocalTransaction</isp.rin.activemq.transaction-support>
<isp.rin.activemq.server-url.primary>tcp://localhost:61616</isp.rin.activemq.server-url.primary>
<isp.rin.activemq.server-url.secondary>tcp://localhost:61616</isp.rin.activemq.server-url.secondary>
<isp.rin.logg.level>DEBUG</isp.rin.logg.level>
<isp.rin.business.logg.level>DEBUG</isp.rin.business.logg.level>
<isp.rin.message.logg.level>INFO</isp.rin.message.logg.level>
</properties>
</profile>
</profiles>
<modelVersion>4.0.0</modelVersion>
<groupId>com.postnord.rin</groupId>
<artifactId>rin-parent</artifactId>
<version>1.4.0</version>
<packaging>pom</packaging>
<name>Parent project for rin-parent</name>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<distribution>repo</distribution>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
</license>
</licenses>
<modules>
<module>rin-common</module>
<module>rin-xsd</module>
<module>rin</module>
<module>rin-ear</module>
</modules>
<properties>
<!-- Explicitly declaring the source encoding eliminates the following message: -->
<!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
resources, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- JBoss dependency versions -->
<version.jboss.maven.plugin>7.3.Final</version.jboss.maven.plugin>
<!-- Define the version of JBoss' Java EE 6 APIs and Tools we want to import. -->
<!-- Certified version of the JBoss EAP components we want to use -->
<version.jboss.bom>1.0.2.Final-redhat-1</version.jboss.bom>
<!--
<version.jboss.as>7.2.0.Final</version.jboss.as>
Alternatively, comment out the above line, and un-comment the
line below to use version 7.2.0.Final-redhat-8 which is a release certified
to work with JBoss EAP 6. It requires you have access to the JBoss EAP 6
maven repository. -->
<!-- --><version.jboss.as>7.2.0.Final-redhat-8</version.jboss.as>
<!--
<version.jboss.spec.javaee.6.0>3.0.2.Final</version.jboss.spec.javaee.6.0>
Alternatively, comment out the above line, and un-comment the
line below to use version 3.0.2.Final-redhat-3 which is a release certified
to work with JBoss EAP 6. It requires you have access to the JBoss EAP 6
maven repository. -->
<!-- <version.jboss.spec.javaee.6.0>3.0.1.Final-redhat-3</version.jboss.spec.javaee.6.0> -->
<version.jboss.spec.javaee.6.0>3.0.1.Final-redhat-1</version.jboss.spec.javaee.6.0>
<!-- other plugin versions -->
<version.compiler.plugin>2.3.2</version.compiler.plugin>
<version.surefire.plugin>2.4.3</version.surefire.plugin>
<version.ear.plugin>2.6</version.ear.plugin>
<version.ejb.plugin>2.3</version.ejb.plugin>
<version.war.plugin>2.1.1</version.war.plugin>
<junit-version>4.12</junit-version>
<!-- maven-compiler-plugin -->
<maven.compiler.plugin>3.5.1</maven.compiler.plugin>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.release.plugin>2.5.3</maven.release.plugin>
<maven.failsafe.plugin>2.19.1</maven.failsafe.plugin>
<maven.surefire.plugin>2.19.1</maven.surefire.plugin>
<jaxb2.maven.plugin.version>1.3</jaxb2.maven.plugin.version>
<RIN.BITC.Name>RegisterItemNotifierBitC-${project.version}</RIN.BITC.Name>
<RIN.PLE.Name>RinPLE-${project.version}</RIN.PLE.Name>
<RIN.DB.PLE.Name>RinDbPLE-${project.version}</RIN.DB.PLE.Name>
<RIN.LOG.PLE.Name>RinLogPLE-${project.version}</RIN.LOG.PLE.Name>
<RIN.EAR.Name>rin</RIN.EAR.Name>
<sonar.plugin.version>2.7</sonar.plugin.version>
<sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin>
<sonar.junit.reportsPath>${basedir}/target/surefire-reports</sonar.junit.reportsPath>
<sonar.language>java</sonar.language>
<sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
<jacoco.version>0.7.6.201602180812</jacoco.version>
<sonar-jacoco-listeners.version>1.4</sonar-jacoco-listeners.version>
<jacoco.outputDir>${basedir}/target/jacoco</jacoco.outputDir>
<jacoco.out.ut.file>jacoco-ut.exec</jacoco.out.ut.file>
<sonar.jacoco.reportPath>${jacoco.outputDir}/${jacoco.out.ut.file}</sonar.jacoco.reportPath>
<jacoco.out.it.file>jacoco-it.exec</jacoco.out.it.file>
<sonar.jacoco.itReportPath>${jacoco.outputDir}/${jacoco.out.it.file}</sonar.jacoco.itReportPath>
<maven.build.helper.plugin.version>1.9</maven.build.helper.plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Define the version of the EJB jar so that we don't need
to repeat ourselves in every module -->
<dependency>
<groupId>com.postnord.rin</groupId>
<artifactId>rin</artifactId>
<!--<version>0.0.1-SNAPSHOT</version>-->
<version>${project.version}</version>
<type>ejb</type>
</dependency>
<!-- Define the version of JBoss' Java EE 6 APIs we want to import.
Any dependencies from org.jboss.spec will have their version defined by this
BOM -->
<!-- JBoss distributes a complete set of Java EE 6 APIs including
a Bill of Materials (BOM). A BOM specifies the versions of a "stack" (or
a collection) of artifacts. We use this here so that we always get the correct
versions of artifacts. Here we use the jboss-javaee-6.0 stack (you can
read this as the JBoss stack of the Java EE 6 APIs). You can actually
use this stack with any version of JBoss AS that implements Java EE 6, not
just JBoss AS 7! -->
<!-- <dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>${version.jboss.spec.javaee.6.0}</version>
<type>pom</type>
<scope>import</scope>
</dependency> -->
<!-- JBoss distributes a complete set of Java EE 6 APIs including
a Bill of Materials (BOM). A BOM specifies the versions of a "stack" (or
a collection) of artifacts. We use this here so that we always get the correct
versions of artifacts. Here we use the jboss-javaee-6.0-with-tools stack
(you can read this as the JBoss stack of the Java EE 6 APIs, with some extras
tools for your project, such as Arquillian for testing) and the jboss-javaee-6.0-with-hibernate
stack you can read this as the JBoss stack of the Java EE 6 APIs, with extras
from the Hibernate family of projects) -->
<dependency>
<groupId>org.jboss.bom</groupId>
<artifactId>jboss-javaee-6.0-with-tools</artifactId>
<version>${version.jboss.bom}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.plugin}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.bom</groupId>
<artifactId>jboss-javaee-6.0-with-hibernate</artifactId>
<version>${version.jboss.bom}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.codehaus.sonar-plugins.java</groupId>
<artifactId>sonar-jacoco-listeners</artifactId>
<version>${sonar-jacoco-listeners.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<repository>
<id>libs-release-local</id>
<name>libs-release-local</name>
<url>http://ella-neserv2.idp.posten.se:37081/artifactory/libs-release-local</url>
</repository>
<snapshotRepository>
<id>libs-snapshot-local</id>
<name>libs-snapshot-local</name>
<url>http://ella-neserv2.idp.posten.se:37081/artifactory/libs-snapshot-local</url>
</snapshotRepository>
</distributionManagement>
<build>
<pluginManagement>
<plugins>
<!-- Compiler plugin enforces Java 1.6 compatibility and
activates annotation processors -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.compiler.plugin}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<!-- The JBoss AS plugin deploys your ear to a local JBoss
AS container -->
<!-- Due to Maven's lack of intelligence with EARs we need
to configure the jboss-as maven plugin to skip deployment for all modules.
We then enable it specifically in the ear module. -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.maven.plugin}</version>
<inherited>true</inherited>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
<configuration>
<argLine>${jacoco.agent.ut.arg}</argLine>
<!-- Specific to generate mapping between tests and covered code -->
<properties>
<property>
<name>listener</name>
<value>org.sonar.java.jacoco.JUnitListener</value>
</property>
</properties>
<!-- test failure ignore -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.plugin}</version>
<configuration>
<argLine>-Xmx1024m -XX:MaxPermSize=256m ${jacoco.agent.it.arg}
</argLine>
<!-- Specific to generate mapping between tests and covered code -->
<properties>
<property>
<name>listener</name>
<value>org.sonar.java.jacoco.JUnitListener</value>
</property>
</properties>
<!-- Let's put failsafe reports with surefire to have access to tests
failures/success reports in sonar -->
<reportsDirectory>${project.build.directory}/surefire-reports
</reportsDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<!-- Prepares a variable, jacoco.agent.ut.arg, that contains the info
to be passed to the JVM hosting the code being tested. -->
<execution>
<id>prepare-ut-agent</id>
<phase>process-test-classes</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>jacoco.agent.ut.arg</propertyName>
<append>true</append>
</configuration>
</execution>
<!-- Prepares a variable, jacoco.agent.it.arg, that contains the info
to be passed to the JVM hosting the code being tested. -->
<execution>
<id>prepare-it-agent</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.itReportPath}</destFile>
<propertyName>jacoco.agent.it.arg</propertyName>
<append>true</append>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
希望它有所帮助!
答案 3 :(得分:1)
我猜不同的线程问题。我已尝试将eventBus用于将数据传递给片段并将其解决。
ControllerFragment(){
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMain(ControllerEvent event) {
if (event.getSelectedEventType() == ControllerEvent.tabControllerEventTypes.refreshCount.ordinal()) {
setUnSendAppleCount(event.getAppleCount());
}
}
}